Skip to content

REF: collect DataFrame.__setitem__ tests #33408

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 8 commits into from
Apr 8, 2020
8 changes: 0 additions & 8 deletions pandas/tests/frame/indexing/test_categorical.py
Original file line number Diff line number Diff line change
Expand Up @@ -391,11 +391,3 @@ def test_loc_indexing_preserves_index_category_dtype(self):

result = df.loc[["a"]].index.levels[0]
tm.assert_index_equal(result, expected)

def test_wrong_length_cat_dtype_raises(self):
# GH29523
cat = pd.Categorical.from_codes([0, 1, 1, 0, 1, 2], ["a", "b", "c"])
df = pd.DataFrame({"bar": range(10)})
err = "Length of values does not match length of index"
with pytest.raises(ValueError, match=err):
df["foo"] = cat
9 changes: 0 additions & 9 deletions pandas/tests/frame/indexing/test_datetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,3 @@ def test_set_reset(self):

df = result.set_index("foo")
tm.assert_index_equal(df.index, idx)

def test_scalar_assignment(self):
# issue #19843
df = pd.DataFrame(index=(0, 1, 2))
df["now"] = pd.Timestamp("20130101", tz="UTC")
expected = pd.DataFrame(
{"now": pd.Timestamp("20130101", tz="UTC")}, index=[0, 1, 2]
)
tm.assert_frame_equal(df, expected)
16 changes: 0 additions & 16 deletions pandas/tests/frame/indexing/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -1921,22 +1921,6 @@ def test_getitem_sparse_column(self):
result = df.loc[:, "A"]
tm.assert_series_equal(result, expected)

def test_setitem_with_sparse_value(self):
# GH8131
df = pd.DataFrame({"c_1": ["a", "b", "c"], "n_1": [1.0, 2.0, 3.0]})
sp_array = SparseArray([0, 0, 1])
df["new_column"] = sp_array
tm.assert_series_equal(
df["new_column"], pd.Series(sp_array, name="new_column"), check_names=False
)

def test_setitem_with_unaligned_sparse_value(self):
df = pd.DataFrame({"c_1": ["a", "b", "c"], "n_1": [1.0, 2.0, 3.0]})
sp_series = pd.Series(SparseArray([0, 0, 1]), index=[2, 1, 0])
df["new_column"] = sp_series
exp = pd.Series(SparseArray([1, 0, 0]), name="new_column")
tm.assert_series_equal(df["new_column"], exp)

def test_setitem_with_unaligned_tz_aware_datetime_column(self):
# GH 12981
# Assignment of unaligned offset-aware datetime series.
Expand Down
50 changes: 46 additions & 4 deletions pandas/tests/frame/indexing/test_setitem.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import numpy as np
import pytest

from pandas import DataFrame, Index, Series
from pandas import Categorical, DataFrame, Index, Series, Timestamp, date_range
import pandas._testing as tm
from pandas.core.arrays import SparseArray

# Column add, remove, delete.


class TestDataFrameMutateColumns:
class TestDataFrameSetItem:
def test_setitem_error_msmgs(self):

# GH 7432
Expand Down Expand Up @@ -84,3 +83,46 @@ def test_setitem_empty_columns(self):
df["X"] = ["x", "y", "z"]
exp = DataFrame(data={"X": ["x", "y", "z"]}, index=["A", "B", "C"])
tm.assert_frame_equal(df, exp)

def test_setitem_dt64_index_empty_columns(self):
rng = date_range("1/1/2000 00:00:00", "1/1/2000 1:59:50", freq="10s")
df = DataFrame(index=np.arange(len(rng)))

df["A"] = rng
assert df["A"].dtype == np.dtype("M8[ns]")

def test_setitem_timestamp_empty_columns(self):
# GH#19843
df = DataFrame(index=range(3))
df["now"] = Timestamp("20130101", tz="UTC")

expected = DataFrame(
[[Timestamp("20130101", tz="UTC")]] * 3, index=[0, 1, 2], columns=["now"],
)
tm.assert_frame_equal(df, expected)

def test_setitem_wrong_length_categorical_dtype_raises(self):
# GH#29523
cat = Categorical.from_codes([0, 1, 1, 0, 1, 2], ["a", "b", "c"])
df = DataFrame(range(10), columns=["bar"])

msg = "Length of values does not match length of index"
with pytest.raises(ValueError, match=msg):
df["foo"] = cat

def test_setitem_with_sparse_value(self):
# GH#8131
df = DataFrame({"c_1": ["a", "b", "c"], "n_1": [1.0, 2.0, 3.0]})
sp_array = SparseArray([0, 0, 1])
df["new_column"] = sp_array

expected = Series(sp_array, name="new_column")
tm.assert_series_equal(df["new_column"], expected)

def test_setitem_with_unaligned_sparse_value(self):
df = DataFrame({"c_1": ["a", "b", "c"], "n_1": [1.0, 2.0, 3.0]})
sp_series = Series(SparseArray([0, 0, 1]), index=[2, 1, 0])

df["new_column"] = sp_series
expected = Series(SparseArray([1, 0, 0]), name="new_column")
tm.assert_series_equal(df["new_column"], expected)
7 changes: 0 additions & 7 deletions pandas/tests/frame/test_timeseries.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,6 @@ def test_frame_ctor_datetime64_column(self):
df = DataFrame({"A": np.random.randn(len(rng)), "B": dates})
assert np.issubdtype(df["B"].dtype, np.dtype("M8[ns]"))

def test_frame_append_datetime64_column(self):
rng = date_range("1/1/2000 00:00:00", "1/1/2000 1:59:50", freq="10s")
df = DataFrame(index=np.arange(len(rng)))

df["A"] = rng
assert np.issubdtype(df["A"].dtype, np.dtype("M8[ns]"))

def test_frame_append_datetime64_col_other_units(self):
n = 100

Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/indexing/multiindex/test_insert.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@


class TestMultiIndexInsertion:
def test_mixed_depth_insert(self):
def test_setitem_mixed_depth(self):
arrays = [
["a", "top", "top", "routine1", "routine1", "routine2"],
["", "OD", "OD", "result1", "result2", "result1"],
Expand Down