Skip to content

Commit 06d49ab

Browse files
committed
wip
1 parent 85301cc commit 06d49ab

File tree

1 file changed

+18
-100
lines changed

1 file changed

+18
-100
lines changed

pandas/tests/frame/indexing/test_indexing.py

Lines changed: 18 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
import numpy as np
1010
import pytest
1111

12-
from pandas._libs import iNaT
1312
from pandas.errors import InvalidIndexError
1413

1514
from pandas.core.dtypes.common import is_integer
@@ -25,7 +24,6 @@
2524
Timestamp,
2625
date_range,
2726
isna,
28-
notna,
2927
to_datetime,
3028
)
3129
import pandas._testing as tm
@@ -832,15 +830,6 @@ def test_setitem_single_column_mixed_datetime(self):
832830
)
833831
tm.assert_series_equal(result, expected)
834832

835-
# GH#16674 iNaT is treated as an integer when given by the user
836-
with tm.assert_produces_warning(
837-
FutureWarning, match="Setting an item of incompatible dtype"
838-
):
839-
df.loc["b", "timestamp"] = iNaT
840-
assert not isna(df.loc["b", "timestamp"])
841-
assert df["timestamp"].dtype == np.object_
842-
assert df.loc["b", "timestamp"] == iNaT
843-
844833
# allow this syntax (as of GH#3216)
845834
df.loc["c", "timestamp"] = np.nan
846835
assert isna(df.loc["c", "timestamp"])
@@ -849,38 +838,6 @@ def test_setitem_single_column_mixed_datetime(self):
849838
df.loc["d", :] = np.nan
850839
assert not isna(df.loc["c", :]).all()
851840

852-
def test_setitem_mixed_datetime(self):
853-
# GH 9336
854-
expected = DataFrame(
855-
{
856-
"a": [0, 0, 0, 0, 13, 14],
857-
"b": [
858-
datetime(2012, 1, 1),
859-
1,
860-
"x",
861-
"y",
862-
datetime(2013, 1, 1),
863-
datetime(2014, 1, 1),
864-
],
865-
}
866-
)
867-
df = DataFrame(0, columns=list("ab"), index=range(6))
868-
df["b"] = pd.NaT
869-
df.loc[0, "b"] = datetime(2012, 1, 1)
870-
with tm.assert_produces_warning(
871-
FutureWarning, match="Setting an item of incompatible dtype"
872-
):
873-
df.loc[1, "b"] = 1
874-
df.loc[[2, 3], "b"] = "x", "y"
875-
A = np.array(
876-
[
877-
[13, np.datetime64("2013-01-01T00:00:00")],
878-
[14, np.datetime64("2014-01-01T00:00:00")],
879-
]
880-
)
881-
df.loc[[4, 5], ["a", "b"]] = A
882-
tm.assert_frame_equal(df, expected)
883-
884841
def test_setitem_frame_float(self, float_frame):
885842
piece = float_frame.loc[float_frame.index[:2], ["A", "B"]]
886843
float_frame.loc[float_frame.index[-2] :, ["A", "B"]] = piece.values
@@ -932,17 +889,6 @@ def test_setitem_frame_mixed_ndarray(self, float_string_frame):
932889
f.loc[key] = piece.values
933890
tm.assert_almost_equal(f.loc[f.index[-2:], ["A", "B"]].values, piece.values)
934891

935-
def test_setitem_frame_upcast(self):
936-
# needs upcasting
937-
df = DataFrame([[1, 2, "foo"], [3, 4, "bar"]], columns=["A", "B", "C"])
938-
df2 = df.copy()
939-
with tm.assert_produces_warning(FutureWarning, match="incompatible dtype"):
940-
df2.loc[:, ["A", "B"]] = df.loc[:, ["A", "B"]] + 0.5
941-
expected = df.reindex(columns=["A", "B"])
942-
expected += 0.5
943-
expected["C"] = df["C"]
944-
tm.assert_frame_equal(df2, expected)
945-
946892
def test_setitem_frame_align(self, float_frame):
947893
piece = float_frame.loc[float_frame.index[:2], ["A", "B"]]
948894
piece.index = float_frame.index[-2:]
@@ -1353,26 +1299,6 @@ def test_loc_expand_empty_frame_keep_midx_names(self):
13531299
)
13541300
tm.assert_frame_equal(df, expected)
13551301

1356-
@pytest.mark.parametrize(
1357-
"val, idxr",
1358-
[
1359-
("x", "a"),
1360-
("x", ["a"]),
1361-
(1, "a"),
1362-
(1, ["a"]),
1363-
],
1364-
)
1365-
def test_loc_setitem_rhs_frame(self, idxr, val):
1366-
# GH#47578
1367-
df = DataFrame({"a": [1, 2]})
1368-
1369-
with tm.assert_produces_warning(
1370-
FutureWarning, match="Setting an item of incompatible dtype"
1371-
):
1372-
df.loc[:, idxr] = DataFrame({"a": [val, 11]}, index=[1, 2])
1373-
expected = DataFrame({"a": [np.nan, val]})
1374-
tm.assert_frame_equal(df, expected)
1375-
13761302
def test_iloc_setitem_enlarge_no_warning(self):
13771303
# GH#47381
13781304
df = DataFrame(columns=["a", "b"])
@@ -1576,22 +1502,6 @@ def test_setitem(self):
15761502
tm.assert_series_equal(df["D"], Series(idx, name="D"))
15771503
del df["D"]
15781504

1579-
# With NaN: because uint64 has no NaN element,
1580-
# the column should be cast to object.
1581-
df2 = df.copy()
1582-
with tm.assert_produces_warning(FutureWarning, match="incompatible dtype"):
1583-
df2.iloc[1, 1] = pd.NaT
1584-
df2.iloc[1, 2] = pd.NaT
1585-
result = df2["B"]
1586-
tm.assert_series_equal(notna(result), Series([True, False, True], name="B"))
1587-
tm.assert_series_equal(
1588-
df2.dtypes,
1589-
Series(
1590-
[np.dtype("uint64"), np.dtype("O"), np.dtype("O")],
1591-
index=["A", "B", "C"],
1592-
),
1593-
)
1594-
15951505

15961506
def test_object_casting_indexing_wraps_datetimelike():
15971507
# GH#31649, check the indexing methods all the way down the stack
@@ -1927,21 +1837,30 @@ class TestSetitemValidation:
19271837
# This is adapted from pandas/tests/arrays/masked/test_indexing.py
19281838
# but checks for warnings instead of errors.
19291839
def _check_setitem_invalid(self, df, invalid, indexer, warn):
1930-
msg = "Setting an item of incompatible dtype is deprecated"
1931-
msg = re.escape(msg)
1932-
19331840
orig_df = df.copy()
19341841

19351842
# iloc
1936-
with tm.assert_produces_warning(warn, match=msg):
1843+
with pytest.raises(TypeError, match="Invalid value"):
19371844
df.iloc[indexer, 0] = invalid
19381845
df = orig_df.copy()
19391846

19401847
# loc
1941-
with tm.assert_produces_warning(warn, match=msg):
1848+
with pytest.raises(TypeError, match="Invalid value"):
19421849
df.loc[indexer, "a"] = invalid
19431850
df = orig_df.copy()
19441851

1852+
def _check_setitem_valid(self, df, invalid, indexer):
1853+
# Just execute, verify no error is raised
1854+
orig_df = df.copy()
1855+
1856+
# iloc
1857+
df.iloc[indexer, 0] = invalid
1858+
df = orig_df.copy()
1859+
1860+
# loc
1861+
df.loc[indexer, "a"] = invalid
1862+
df = orig_df.copy()
1863+
19451864
_invalid_scalars = [
19461865
1 + 2j,
19471866
"True",
@@ -1959,20 +1878,19 @@ def _check_setitem_invalid(self, df, invalid, indexer, warn):
19591878
@pytest.mark.parametrize("indexer", _indexers)
19601879
def test_setitem_validation_scalar_bool(self, invalid, indexer):
19611880
df = DataFrame({"a": [True, False, False]}, dtype="bool")
1962-
self._check_setitem_invalid(df, invalid, indexer, FutureWarning)
1881+
self._check_setitem_invalid(df, invalid, indexer)
19631882

19641883
@pytest.mark.parametrize("invalid", _invalid_scalars + [True, 1.5, np.float64(1.5)])
19651884
@pytest.mark.parametrize("indexer", _indexers)
19661885
def test_setitem_validation_scalar_int(self, invalid, any_int_numpy_dtype, indexer):
19671886
df = DataFrame({"a": [1, 2, 3]}, dtype=any_int_numpy_dtype)
19681887
if isna(invalid) and invalid is not pd.NaT and not np.isnat(invalid):
1969-
warn = None
1888+
self._check_setitem_valid(df, invalid, indexer)
19701889
else:
1971-
warn = FutureWarning
1972-
self._check_setitem_invalid(df, invalid, indexer, warn)
1890+
self._check_setitem_invalid(df, invalid, indexer)
19731891

19741892
@pytest.mark.parametrize("invalid", _invalid_scalars + [True])
19751893
@pytest.mark.parametrize("indexer", _indexers)
19761894
def test_setitem_validation_scalar_float(self, invalid, float_numpy_dtype, indexer):
19771895
df = DataFrame({"a": [1, 2, None]}, dtype=float_numpy_dtype)
1978-
self._check_setitem_invalid(df, invalid, indexer, FutureWarning)
1896+
self._check_setitem_invalid(df, invalid, indexer)

0 commit comments

Comments
 (0)