Skip to content

Commit ad4f16c

Browse files
committed
REF: collect DataFrame.__setitem__ tests (pandas-dev#33408)
1 parent f40886e commit ad4f16c

File tree

6 files changed

+47
-45
lines changed

6 files changed

+47
-45
lines changed

pandas/tests/frame/indexing/test_categorical.py

-8
Original file line numberDiff line numberDiff line change
@@ -391,11 +391,3 @@ def test_loc_indexing_preserves_index_category_dtype(self):
391391

392392
result = df.loc[["a"]].index.levels[0]
393393
tm.assert_index_equal(result, expected)
394-
395-
def test_wrong_length_cat_dtype_raises(self):
396-
# GH29523
397-
cat = pd.Categorical.from_codes([0, 1, 1, 0, 1, 2], ["a", "b", "c"])
398-
df = pd.DataFrame({"bar": range(10)})
399-
err = "Length of values does not match length of index"
400-
with pytest.raises(ValueError, match=err):
401-
df["foo"] = cat

pandas/tests/frame/indexing/test_datetime.py

-9
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,3 @@ def test_set_reset(self):
4444

4545
df = result.set_index("foo")
4646
tm.assert_index_equal(df.index, idx)
47-
48-
def test_scalar_assignment(self):
49-
# issue #19843
50-
df = pd.DataFrame(index=(0, 1, 2))
51-
df["now"] = pd.Timestamp("20130101", tz="UTC")
52-
expected = pd.DataFrame(
53-
{"now": pd.Timestamp("20130101", tz="UTC")}, index=[0, 1, 2]
54-
)
55-
tm.assert_frame_equal(df, expected)

pandas/tests/frame/indexing/test_indexing.py

-16
Original file line numberDiff line numberDiff line change
@@ -1921,22 +1921,6 @@ def test_getitem_sparse_column(self):
19211921
result = df.loc[:, "A"]
19221922
tm.assert_series_equal(result, expected)
19231923

1924-
def test_setitem_with_sparse_value(self):
1925-
# GH8131
1926-
df = pd.DataFrame({"c_1": ["a", "b", "c"], "n_1": [1.0, 2.0, 3.0]})
1927-
sp_array = SparseArray([0, 0, 1])
1928-
df["new_column"] = sp_array
1929-
tm.assert_series_equal(
1930-
df["new_column"], pd.Series(sp_array, name="new_column"), check_names=False
1931-
)
1932-
1933-
def test_setitem_with_unaligned_sparse_value(self):
1934-
df = pd.DataFrame({"c_1": ["a", "b", "c"], "n_1": [1.0, 2.0, 3.0]})
1935-
sp_series = pd.Series(SparseArray([0, 0, 1]), index=[2, 1, 0])
1936-
df["new_column"] = sp_series
1937-
exp = pd.Series(SparseArray([1, 0, 0]), name="new_column")
1938-
tm.assert_series_equal(df["new_column"], exp)
1939-
19401924
def test_setitem_with_unaligned_tz_aware_datetime_column(self):
19411925
# GH 12981
19421926
# Assignment of unaligned offset-aware datetime series.

pandas/tests/frame/indexing/test_setitem.py

+46-4
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
import numpy as np
22
import pytest
33

4-
from pandas import DataFrame, Index, Series
4+
from pandas import Categorical, DataFrame, Index, Series, Timestamp, date_range
55
import pandas._testing as tm
6+
from pandas.core.arrays import SparseArray
67

7-
# Column add, remove, delete.
88

9-
10-
class TestDataFrameMutateColumns:
9+
class TestDataFrameSetItem:
1110
def test_setitem_error_msmgs(self):
1211

1312
# GH 7432
@@ -84,3 +83,46 @@ def test_setitem_empty_columns(self):
8483
df["X"] = ["x", "y", "z"]
8584
exp = DataFrame(data={"X": ["x", "y", "z"]}, index=["A", "B", "C"])
8685
tm.assert_frame_equal(df, exp)
86+
87+
def test_setitem_dt64_index_empty_columns(self):
88+
rng = date_range("1/1/2000 00:00:00", "1/1/2000 1:59:50", freq="10s")
89+
df = DataFrame(index=np.arange(len(rng)))
90+
91+
df["A"] = rng
92+
assert df["A"].dtype == np.dtype("M8[ns]")
93+
94+
def test_setitem_timestamp_empty_columns(self):
95+
# GH#19843
96+
df = DataFrame(index=range(3))
97+
df["now"] = Timestamp("20130101", tz="UTC")
98+
99+
expected = DataFrame(
100+
[[Timestamp("20130101", tz="UTC")]] * 3, index=[0, 1, 2], columns=["now"],
101+
)
102+
tm.assert_frame_equal(df, expected)
103+
104+
def test_setitem_wrong_length_categorical_dtype_raises(self):
105+
# GH#29523
106+
cat = Categorical.from_codes([0, 1, 1, 0, 1, 2], ["a", "b", "c"])
107+
df = DataFrame(range(10), columns=["bar"])
108+
109+
msg = "Length of values does not match length of index"
110+
with pytest.raises(ValueError, match=msg):
111+
df["foo"] = cat
112+
113+
def test_setitem_with_sparse_value(self):
114+
# GH#8131
115+
df = DataFrame({"c_1": ["a", "b", "c"], "n_1": [1.0, 2.0, 3.0]})
116+
sp_array = SparseArray([0, 0, 1])
117+
df["new_column"] = sp_array
118+
119+
expected = Series(sp_array, name="new_column")
120+
tm.assert_series_equal(df["new_column"], expected)
121+
122+
def test_setitem_with_unaligned_sparse_value(self):
123+
df = DataFrame({"c_1": ["a", "b", "c"], "n_1": [1.0, 2.0, 3.0]})
124+
sp_series = Series(SparseArray([0, 0, 1]), index=[2, 1, 0])
125+
126+
df["new_column"] = sp_series
127+
expected = Series(SparseArray([1, 0, 0]), name="new_column")
128+
tm.assert_series_equal(df["new_column"], expected)

pandas/tests/frame/test_timeseries.py

-7
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,6 @@ def test_frame_ctor_datetime64_column(self):
1313
df = DataFrame({"A": np.random.randn(len(rng)), "B": dates})
1414
assert np.issubdtype(df["B"].dtype, np.dtype("M8[ns]"))
1515

16-
def test_frame_append_datetime64_column(self):
17-
rng = date_range("1/1/2000 00:00:00", "1/1/2000 1:59:50", freq="10s")
18-
df = DataFrame(index=np.arange(len(rng)))
19-
20-
df["A"] = rng
21-
assert np.issubdtype(df["A"].dtype, np.dtype("M8[ns]"))
22-
2316
def test_frame_append_datetime64_col_other_units(self):
2417
n = 100
2518

pandas/tests/indexing/multiindex/test_insert.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66

77
class TestMultiIndexInsertion:
8-
def test_mixed_depth_insert(self):
8+
def test_setitem_mixed_depth(self):
99
arrays = [
1010
["a", "top", "top", "routine1", "routine1", "routine2"],
1111
["", "OD", "OD", "result1", "result2", "result1"],

0 commit comments

Comments
 (0)