-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
BUG: Fix issue with datetime[ns, tz] input in Block.setitem GH32395 #32479
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
Changes from 21 commits
198474d
6eba1c9
11b4f29
1b3cba1
d83cff9
4e66228
ac85aa3
2d25c7a
3e78985
7e600c7
05a788c
b136ce3
d433d7c
00199f6
01f01fd
8cfa045
4f8fccd
0f8a913
9d4d1df
a01a676
26f8ed3
3caf161
8d9e6d7
fbc31c8
c7e7cf5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
""" test label based indexing with loc """ | ||
from datetime import timezone | ||
from io import StringIO | ||
import re | ||
|
||
|
@@ -1106,3 +1107,133 @@ def test_loc_with_period_index_indexer(): | |
tm.assert_frame_equal(df, df.loc[list(idx)]) | ||
tm.assert_frame_equal(df.iloc[0:5], df.loc[idx[0:5]]) | ||
tm.assert_frame_equal(df, df.loc[list(idx)]) | ||
|
||
|
||
def test_loc_setitem_df_datetime64tz_full_column_with_index(): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. all of this need to move to pandas/tests/extension/setitem BUT you need to use the fixtures, so you are like writing ONE test. |
||
# GH#32395 ea assignments with an index raise TypeError | ||
expected = pd.DataFrame( | ||
pd.date_range("2020-01-01", "2020-01-06", 6, tz=timezone.utc), columns=["data"] | ||
) | ||
result = pd.DataFrame(index=expected.index) | ||
result.loc[expected.index, "data"] = expected["data"] | ||
tm.assert_frame_equal(expected, result) | ||
|
||
|
||
def test_loc_setitem_df_datetime64tz_slice(): | ||
# GH#32395 ea assignments with an index raise TypeError | ||
df = pd.DataFrame( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. add the issue number as a comment |
||
pd.date_range("2020-01-01", "2020-01-06", 6, tz=timezone.utc), columns=["data"] | ||
) | ||
result = pd.DataFrame(index=df.index) | ||
expected = pd.DataFrame( | ||
[ | ||
pd.Timestamp("2020-01-01", tz=timezone.utc), | ||
pd.Timestamp("2020-01-02", tz=timezone.utc), | ||
pd.Timestamp("2020-01-03", tz=timezone.utc), | ||
pd.NaT, | ||
pd.NaT, | ||
pd.NaT, | ||
], | ||
columns=["data"], | ||
dtype="object", | ||
) | ||
result.loc[df.index[:3], "data"] = df["data"][:3] | ||
tm.assert_frame_equal(expected, result) | ||
|
||
|
||
def test_loc_setitem_series_datetime64tz_full_with_index(): | ||
# GH#32395 ea assignments with an index raise TypeError | ||
expected = pd.Series( | ||
pd.date_range("2020-01-01", "2020-01-06", 6, tz=timezone.utc), name="data" | ||
) | ||
result = pd.Series(index=expected.index, dtype="object", name="data") | ||
result.loc[expected.index] = expected | ||
tm.assert_series_equal(expected, result) | ||
|
||
|
||
def test_loc_setitem_series_datetime64tz_slice(): | ||
# GH#32395 ea assignments with an index raise TypeError | ||
dates = pd.date_range("2020-01-01", "2020-01-06", 6, tz=timezone.utc) | ||
s1 = pd.Series(dates, name="data") | ||
result = pd.Series(index=s1.index, dtype="object", name="data") | ||
expected = pd.Series(dates, name="data", dtype="object") | ||
expected[-3:] = pd.NaT | ||
result.loc[s1.index[:3]] = s1[:3] | ||
tm.assert_series_equal(expected, result) | ||
|
||
|
||
def test_loc_setitem_series_timedelta64_full_with_index(): | ||
# GH#32395 ea assignments with an index raise TypeError | ||
expected = pd.Series(pd.timedelta_range(start="1 day", periods=4), name="data") | ||
result = pd.Series(index=expected.index, dtype="object", name="data") | ||
result.loc[expected.index] = expected | ||
tm.assert_series_equal(expected, result) | ||
|
||
|
||
def test_loc_setitem_df_period_full_column_with_index(): | ||
# GH#32395 ea assignments with an index raise TypeError | ||
expected = pd.DataFrame( | ||
pd.period_range(start="2020Q1", periods=5), columns=["data"] | ||
) | ||
result = pd.DataFrame(index=expected.index) | ||
result.loc[expected.index, "data"] = expected["data"] | ||
tm.assert_frame_equal(expected, result) | ||
|
||
|
||
def test_loc_setitem_series_interval_full_with_index(): | ||
# GH#32395 ea assignments with an index raise TypeError | ||
expected = pd.Series(pd.interval_range(start=0, end=5), name="data") | ||
result = pd.Series(index=expected.index, dtype="object", name="data") | ||
result.loc[expected.index] = expected | ||
tm.assert_series_equal(expected, result) | ||
|
||
|
||
def test_loc_setitem_df_sparse_full_column_with_index(): | ||
# GH#32395 ea assignments with an index raise TypeError | ||
expected = pd.DataFrame(np.random.randn(100), columns=["data"]) | ||
expected.iloc[5:95] = np.nan | ||
expected = expected.astype(pd.SparseDtype("int", np.nan)) | ||
result = pd.DataFrame(index=expected.index) | ||
result.loc[expected.index, "data"] = expected["data"] | ||
tm.assert_frame_equal(expected, result) | ||
|
||
|
||
def test_loc_setitem_series_categorical_full_with_index(): | ||
# GH#32395 ea assignments with an index raise TypeError | ||
expected = pd.Series(pd.Categorical([1] * 10 + [2] * 5 + [3] * 10), name="data") | ||
result = pd.Series(index=expected.index, dtype="object", name="data") | ||
result.loc[expected.index] = expected | ||
tm.assert_series_equal(expected, result) | ||
|
||
|
||
def test_loc_setitem_series_categorical_slice(): | ||
# GH#32395 ea assignments with an index raise TypeError | ||
s1 = pd.Series(pd.Categorical([1] * 10 + [2] * 5 + [3] * 10), name="data") | ||
result = pd.Series(index=s1.index, dtype="object", name="data") | ||
expected = pd.Series( | ||
pd.Categorical([1] * 10 + [2] * 5 + [np.nan] * 10), name="data", dtype=object | ||
) | ||
result.loc[s1.index[:15]] = s1[:15] | ||
tm.assert_series_equal(expected, result) | ||
|
||
|
||
def test_loc_setitem_df_interval_slice(): | ||
# GH#32395 ea assignments with an index raise TypeError | ||
intervals = pd.interval_range(start=0, end=5) | ||
df = pd.DataFrame(intervals, columns=["data"]) | ||
result = pd.DataFrame(index=df.index) | ||
expected = pd.DataFrame(intervals, columns=["data"], dtype="object") | ||
expected.data[-2:] = np.nan | ||
result.loc[df.index[:3], "data"] = df["data"][:3] | ||
tm.assert_frame_equal(expected, result) | ||
|
||
|
||
def test_loc_setitem_series_period_slice(): | ||
# GH#32395 ea assignments with an index raise TypeError | ||
periods = pd.period_range(start="2020Q1", periods=5) | ||
s1 = pd.Series(periods, name="data") | ||
result = pd.Series(index=s1.index, dtype="object", name="data") | ||
expected = pd.Series(periods, name="data", dtype=object,) | ||
expected[-2:] = np.nan | ||
result.loc[s1.index[:3]] = s1[:3] | ||
tm.assert_series_equal(expected, result) |
Uh oh!
There was an error while loading. Please reload this page.