Skip to content

TST: collect indexing tests by method #40011

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Feb 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions pandas/tests/frame/methods/test_update.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,3 +137,12 @@ def test_update_datetime_tz(self):
result.update(result)
expected = DataFrame([pd.Timestamp("2019", tz="UTC")])
tm.assert_frame_equal(result, expected)

def test_update_with_different_dtype(self):
# GH#3217
df = DataFrame({"a": [1, 3], "b": [np.nan, 2]})
df["c"] = np.nan
df["c"].update(Series(["foo"], index=[0]))

expected = DataFrame({"a": [1, 3], "b": [np.nan, 2], "c": ["foo", np.nan]})
tm.assert_frame_equal(df, expected)
16 changes: 16 additions & 0 deletions pandas/tests/indexing/test_chaining_and_caching.py
Original file line number Diff line number Diff line change
Expand Up @@ -457,3 +457,19 @@ def test_cache_updating2(self):
tm.assert_frame_equal(df, expected)
expected = Series([0, 0, 0, 2, 0], name="f")
tm.assert_series_equal(df.f, expected)

def test_iloc_setitem_chained_assignment(self):
# GH#3970
with option_context("chained_assignment", None):
df = DataFrame({"aa": range(5), "bb": [2.2] * 5})
df["cc"] = 0.0

ck = [True] * len(df)

df["bb"].iloc[0] = 0.13

# TODO: unused
df_tmp = df.iloc[ck] # noqa

df["bb"].iloc[0] = 0.15
assert df["bb"].iloc[0] == 0.15
85 changes: 0 additions & 85 deletions pandas/tests/indexing/test_datetime.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
import numpy as np
import pytest

import pandas as pd
from pandas import (
DataFrame,
Expand Down Expand Up @@ -55,28 +52,6 @@ def test_indexing_fast_xs(self):
expected = df.iloc[4:]
tm.assert_frame_equal(result, expected)

def test_setitem_with_expansion(self):
# indexing - setting an element
df = DataFrame(
data=pd.to_datetime(["2015-03-30 20:12:32", "2015-03-12 00:11:11"]),
columns=["time"],
)
df["new_col"] = ["new", "old"]
df.time = df.set_index("time").index.tz_localize("UTC")
v = df[df.new_col == "new"].set_index("time").index.tz_convert("US/Pacific")

# trying to set a single element on a part of a different timezone
# this converts to object
df2 = df.copy()
df2.loc[df2.new_col == "new", "time"] = v

expected = Series([v[0], df.loc[1, "time"]], name="time")
tm.assert_series_equal(df2.time, expected)

v = df.loc[df.new_col == "new", "time"] + pd.Timedelta("1s")
df.loc[df.new_col == "new", "time"] = v
tm.assert_series_equal(df.loc[df.new_col == "new", "time"], v)

def test_consistency_with_tz_aware_scalar(self):
# xef gh-12938
# various ways of indexing the same tz-aware scalar
Expand Down Expand Up @@ -163,48 +138,6 @@ def test_indexing_with_datetimeindex_tz(self):
expected = Series([0, 5], index=index)
tm.assert_series_equal(result, expected)

@pytest.mark.parametrize("to_period", [True, False])
def test_loc_getitem_listlike_of_datetimelike_keys(self, to_period):
# GH 11497

idx = date_range("2011-01-01", "2011-01-02", freq="D", name="idx")
if to_period:
idx = idx.to_period("D")
ser = Series([0.1, 0.2], index=idx, name="s")

keys = [Timestamp("2011-01-01"), Timestamp("2011-01-02")]
if to_period:
keys = [x.to_period("D") for x in keys]
result = ser.loc[keys]
exp = Series([0.1, 0.2], index=idx, name="s")
if not to_period:
exp.index = exp.index._with_freq(None)
tm.assert_series_equal(result, exp, check_index_type=True)

keys = [
Timestamp("2011-01-02"),
Timestamp("2011-01-02"),
Timestamp("2011-01-01"),
]
if to_period:
keys = [x.to_period("D") for x in keys]
exp = Series(
[0.2, 0.2, 0.1], index=Index(keys, name="idx", dtype=idx.dtype), name="s"
)
result = ser.loc[keys]
tm.assert_series_equal(result, exp, check_index_type=True)

keys = [
Timestamp("2011-01-03"),
Timestamp("2011-01-02"),
Timestamp("2011-01-03"),
]
if to_period:
keys = [x.to_period("D") for x in keys]

with pytest.raises(KeyError, match="with any missing labels"):
ser.loc[keys]

def test_nanosecond_getitem_setitem_with_tz(self):
# GH 11679
data = ["2016-06-28 08:30:00.123456789"]
Expand All @@ -219,24 +152,6 @@ def test_nanosecond_getitem_setitem_with_tz(self):
expected = DataFrame(-1, index=index, columns=["a"])
tm.assert_frame_equal(result, expected)

def test_loc_setitem_with_expansion_and_existing_dst(self):
# GH 18308
start = Timestamp("2017-10-29 00:00:00+0200", tz="Europe/Madrid")
end = Timestamp("2017-10-29 03:00:00+0100", tz="Europe/Madrid")
ts = Timestamp("2016-10-10 03:00:00", tz="Europe/Madrid")
idx = pd.date_range(start, end, closed="left", freq="H")
assert ts not in idx # i.e. result.loc setitem is with-expansion

result = DataFrame(index=idx, columns=["value"])
result.loc[ts, "value"] = 12
expected = DataFrame(
[np.nan] * len(idx) + [12],
index=idx.append(pd.DatetimeIndex([ts])),
columns=["value"],
dtype=object,
)
tm.assert_frame_equal(result, expected)

def test_getitem_millisecond_resolution(self, frame_or_series):
# GH#33589

Expand Down
134 changes: 52 additions & 82 deletions pandas/tests/indexing/test_floats.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def check(self, result, original, indexer, getitem):
tm.makePeriodIndex,
],
)
def test_scalar_non_numeric(self, index_func, frame_or_series):
def test_scalar_non_numeric(self, index_func, frame_or_series, indexer_sl):

# GH 4892
# float_indexers should raise exceptions
Expand All @@ -61,10 +61,7 @@ def test_scalar_non_numeric(self, index_func, frame_or_series):

# getting
with pytest.raises(KeyError, match="^3.0$"):
s[3.0]

with pytest.raises(KeyError, match="^3.0$"):
s.loc[3.0]
indexer_sl(s)[3.0]

# contains
assert 3.0 not in s
Expand All @@ -88,11 +85,7 @@ def test_scalar_non_numeric(self, index_func, frame_or_series):
else:

s2 = s.copy()
s2.loc[3.0] = 10
assert s2.index.is_object()

s2 = s.copy()
s2[3.0] = 0
indexer_sl(s2)[3.0] = 10
assert s2.index.is_object()

@pytest.mark.parametrize(
Expand All @@ -114,44 +107,44 @@ def test_scalar_non_numeric_series_fallback(self, index_func):
with pytest.raises(KeyError, match="^3.0$"):
s[3.0]

def test_scalar_with_mixed(self):
def test_scalar_with_mixed(self, indexer_sl):

s2 = Series([1, 2, 3], index=["a", "b", "c"])
s3 = Series([1, 2, 3], index=["a", "b", 1.5])

# lookup in a pure string index with an invalid indexer

with pytest.raises(KeyError, match="^1.0$"):
s2[1.0]
indexer_sl(s2)[1.0]

with pytest.raises(KeyError, match=r"^1\.0$"):
s2.loc[1.0]
indexer_sl(s2)[1.0]

result = s2.loc["b"]
result = indexer_sl(s2)["b"]
expected = 2
assert result == expected

# mixed index so we have label
# indexing
with pytest.raises(KeyError, match="^1.0$"):
s3[1.0]
indexer_sl(s3)[1.0]

result = s3[1]
expected = 2
assert result == expected
if indexer_sl is not tm.loc:
# __getitem__ falls back to positional
result = s3[1]
expected = 2
assert result == expected

with pytest.raises(KeyError, match=r"^1\.0$"):
s3.loc[1.0]
indexer_sl(s3)[1.0]

result = s3.loc[1.5]
result = indexer_sl(s3)[1.5]
expected = 3
assert result == expected

@pytest.mark.parametrize(
"idxr,getitem", [(lambda x: x.loc, False), (lambda x: x, True)]
)
@pytest.mark.parametrize("index_func", [tm.makeIntIndex, tm.makeRangeIndex])
def test_scalar_integer(self, index_func, frame_or_series, idxr, getitem):
def test_scalar_integer(self, index_func, frame_or_series, indexer_sl):
getitem = indexer_sl is not tm.loc

# test how scalar float indexers work on int indexes

Expand All @@ -161,7 +154,7 @@ def test_scalar_integer(self, index_func, frame_or_series, idxr, getitem):

# coerce to equal int

result = idxr(obj)[3.0]
result = indexer_sl(obj)[3.0]
self.check(result, obj, 3, getitem)

if isinstance(obj, Series):
Expand All @@ -178,12 +171,12 @@ def compare(x, y):
expected = Series(100.0, index=range(len(obj)), name=3)

s2 = obj.copy()
idxr(s2)[3.0] = 100
indexer_sl(s2)[3.0] = 100

result = idxr(s2)[3.0]
result = indexer_sl(s2)[3.0]
compare(result, expected)

result = idxr(s2)[3]
result = indexer_sl(s2)[3]
compare(result, expected)

@pytest.mark.parametrize("index_func", [tm.makeIntIndex, tm.makeRangeIndex])
Expand All @@ -204,7 +197,8 @@ def test_scalar_float(self, frame_or_series):

# assert all operations except for iloc are ok
indexer = index[3]
for idxr, getitem in [(lambda x: x.loc, False), (lambda x: x, True)]:
for idxr in [tm.loc, tm.setitem]:
getitem = idxr is not tm.loc

# getting
result = idxr(s)[indexer]
Expand Down Expand Up @@ -242,7 +236,7 @@ def test_scalar_float(self, frame_or_series):
],
)
@pytest.mark.parametrize("idx", [slice(3.0, 4), slice(3, 4.0), slice(3.0, 4.0)])
def test_slice_non_numeric(self, index_func, idx, frame_or_series):
def test_slice_non_numeric(self, index_func, idx, frame_or_series, indexer_sli):

# GH 4892
# float_indexers should raise exceptions
Expand All @@ -252,38 +246,28 @@ def test_slice_non_numeric(self, index_func, idx, frame_or_series):
s = gen_obj(frame_or_series, index)

# getitem
msg = (
"cannot do positional indexing "
fr"on {type(index).__name__} with these indexers \[(3|4)\.0\] of "
"type float"
)
if indexer_sli is tm.iloc:
msg = (
"cannot do positional indexing "
fr"on {type(index).__name__} with these indexers \[(3|4)\.0\] of "
"type float"
)
else:
msg = (
"cannot do slice indexing "
fr"on {type(index).__name__} with these indexers "
r"\[(3|4)(\.0)?\] "
r"of type (float|int)"
)
with pytest.raises(TypeError, match=msg):
s.iloc[idx]

msg = (
"cannot do (slice|positional) indexing "
fr"on {type(index).__name__} with these indexers "
r"\[(3|4)(\.0)?\] "
r"of type (float|int)"
)
for idxr in [lambda x: x.loc, lambda x: x.iloc, lambda x: x]:
with pytest.raises(TypeError, match=msg):
idxr(s)[idx]
indexer_sli(s)[idx]

# setitem
msg = "slice indices must be integers or None or have an __index__ method"
if indexer_sli is tm.iloc:
# otherwise we keep the same message as above
msg = "slice indices must be integers or None or have an __index__ method"
with pytest.raises(TypeError, match=msg):
s.iloc[idx] = 0

msg = (
"cannot do (slice|positional) indexing "
fr"on {type(index).__name__} with these indexers "
r"\[(3|4)(\.0)?\] "
r"of type (float|int)"
)
for idxr in [lambda x: x.loc, lambda x: x]:
with pytest.raises(TypeError, match=msg):
idxr(s)[idx] = 0
indexer_sli(s)[idx] = 0

def test_slice_integer(self):

Expand Down Expand Up @@ -469,25 +453,24 @@ def test_float_slice_getitem_with_integer_index_raises(self, idx, index_func):
s[idx]

@pytest.mark.parametrize("idx", [slice(3.0, 4), slice(3, 4.0), slice(3.0, 4.0)])
def test_slice_float(self, idx, frame_or_series):
def test_slice_float(self, idx, frame_or_series, indexer_sl):

# same as above, but for floats
index = Index(np.arange(5.0)) + 0.1
s = gen_obj(frame_or_series, index)

expected = s.iloc[3:4]
for idxr in [lambda x: x.loc, lambda x: x]:

# getitem
result = idxr(s)[idx]
assert isinstance(result, type(s))
tm.assert_equal(result, expected)
# getitem
result = indexer_sl(s)[idx]
assert isinstance(result, type(s))
tm.assert_equal(result, expected)

# setitem
s2 = s.copy()
idxr(s2)[idx] = 0
result = idxr(s2)[idx].values.ravel()
assert (result == 0).all()
# setitem
s2 = s.copy()
indexer_sl(s2)[idx] = 0
result = indexer_sl(s2)[idx].values.ravel()
assert (result == 0).all()

def test_floating_index_doc_example(self):

Expand Down Expand Up @@ -564,19 +547,6 @@ def test_floating_misc(self, indexer_sl):
result = indexer_sl(s)[[2.5]]
tm.assert_series_equal(result, Series([1], index=[2.5]))

def test_floating_tuples(self):
# see gh-13509
s = Series([(1, 1), (2, 2), (3, 3)], index=[0.0, 0.1, 0.2], name="foo")

result = s[0.0]
assert result == (1, 1)

expected = Series([(1, 1), (2, 2)], index=[0.0, 0.0], name="foo")
s = Series([(1, 1), (2, 2), (3, 3)], index=[0.0, 0.0, 0.2], name="foo")

result = s[0.0]
tm.assert_series_equal(result, expected)

def test_float64index_slicing_bug(self):
# GH 5557, related to slicing a float index
ser = {
Expand Down
Loading