-
-
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 7 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 |
---|---|---|
|
@@ -24,6 +24,7 @@ Version 1.0 | |
.. toctree:: | ||
:maxdepth: 2 | ||
|
||
v1.0.4 | ||
v1.0.3 | ||
v1.0.2 | ||
v1.0.1 | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,4 +26,4 @@ Bug fixes | |
Contributors | ||
~~~~~~~~~~~~ | ||
|
||
.. contributors:: v1.0.2..v1.0.3|HEAD | ||
.. contributors:: v1.0.2..v1.0.3 | ||
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. revert this |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
|
||
.. _whatsnew_104: | ||
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. pls remove this and all references to 1.0.4, just put a note in 1.1 |
||
|
||
What's new in 1.0.4 (April 15, 2020) | ||
------------------------------------ | ||
|
||
These are the changes in pandas 1.0.4. See :ref:`release` for a full changelog | ||
including other versions of pandas. | ||
|
||
{{ header }} | ||
|
||
.. --------------------------------------------------------------------------- | ||
|
||
.. _whatsnew_104.regressions: | ||
|
||
Fixed regressions | ||
~~~~~~~~~~~~~~~~~ | ||
- Fixed regression in :meth:`DataFrame.loc`, :meth:`Series.loc` throwing an error when a ``datetime64[ns, tz]`` value is provided (:issue:`32395`) | ||
|
||
.. _whatsnew_104.bug_fixes: | ||
|
||
Bug fixes | ||
~~~~~~~~~ | ||
|
||
Contributors | ||
~~~~~~~~~~~~ | ||
|
||
.. contributors:: v1.0.3..v1.0.4|HEAD |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1098,14 +1098,45 @@ def test_loc_datetimelike_mismatched_dtypes(): | |
df["a"].loc[tdi] | ||
|
||
|
||
def test_loc_setitem_df_datetime64tz_column_with_index(): | ||
def test_loc_with_period_index_indexer(): | ||
# GH#4125 | ||
idx = pd.period_range("2002-01", "2003-12", freq="M") | ||
df = pd.DataFrame(np.random.randn(24, 10), index=idx) | ||
tm.assert_frame_equal(df, df.loc[idx]) | ||
tm.assert_frame_equal(df, df.loc[list(idx)]) | ||
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. |
||
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 a comment with the issue number |
||
pd.date_range("2020-01-01", "2020-01-06", 6, tz=timezone.utc), columns=["data"] | ||
) | ||
df2 = pd.DataFrame(index=df.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. use result = as its not really clear what you are comparing here |
||
df2.loc[df.index, "data"] = df["data"] | ||
tm.assert_numpy_array_equal(np.array(df.data), np.array(df2.data)) | ||
assert df2.data.dtype == np.object | ||
tm.assert_frame_equal(df, df2) | ||
|
||
|
||
def test_loc_setitem_df_datetime64tz_slice(): | ||
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"] | ||
) | ||
df2 = 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", | ||
) | ||
df2.loc[df.index[:3], "data"] = df["data"][:3] | ||
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. use |
||
tm.assert_frame_equal(df2, expected) | ||
|
||
|
||
def test_loc_setitem_df_datetime64tz_column_without_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. you don't need to repeat things like this, instead we aleady have generic tests in : pandas/tests/extension//base/setitem.py 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. @h-vishal this is the key comment. Please do not repeat things like this. We already have fixtures for all of these. 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. @jreback I have pushed a commit with the requested changes. 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. it doesn't look like anything has changed. there should be NO tests here, rather in pandas/tests/extension/base/setitem |
||
|
@@ -1114,24 +1145,110 @@ def test_loc_setitem_df_datetime64tz_column_without_index(): | |
) | ||
df2 = pd.DataFrame(index=df.index) | ||
df2.loc[:, "data"] = df["data"] | ||
tm.assert_series_equal(df.data, df2.data) | ||
tm.assert_frame_equal(df, df2) | ||
|
||
|
||
def test_loc_setitem_series_datetime64tz_with_index(): | ||
def test_loc_setitem_series_datetime64tz_full_with_index(): | ||
s1 = pd.Series( | ||
pd.date_range("2020-01-01", "2020-01-06", 6, tz=timezone.utc), name="data" | ||
) | ||
s2 = pd.Series(index=s1.index, dtype=np.object, name="data") | ||
s2 = pd.Series(index=s1.index, dtype="object", name="data") | ||
s2.loc[s1.index] = s1 | ||
tm.assert_numpy_array_equal(np.array(s1), np.array(s2)) | ||
assert s2.dtype == np.object | ||
tm.assert_series_equal(s1, s2) | ||
|
||
|
||
def test_loc_setitem_series_datetime64tz_slice(): | ||
s1 = pd.Series( | ||
pd.date_range("2020-01-01", "2020-01-06", 6, tz=timezone.utc), name="data" | ||
) | ||
s2 = pd.Series(index=s1.index, dtype="object", name="data") | ||
expected = pd.Series( | ||
[ | ||
*pd.date_range("2020-01-01", "2020-01-03", 3, tz=timezone.utc), | ||
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. why the "*" 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. i guess do
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. @jbrockmendel okay, if that is preferred. 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. @jbrockmendel please note the |
||
np.nan, | ||
np.nan, | ||
np.nan, | ||
], | ||
name="data", | ||
dtype="object", | ||
) | ||
s2.loc[s1.index[:3]] = s1[:3] | ||
tm.assert_series_equal(expected, s2) | ||
|
||
|
||
def test_loc_setitem_series_datetime64tz_without_index(): | ||
s1 = pd.Series( | ||
pd.date_range("2020-01-01", "2020-01-06", 6, tz=timezone.utc), name="data" | ||
) | ||
s2 = pd.Series(index=s1.index, dtype=np.object, name="data") | ||
s2 = pd.Series(index=s1.index, dtype="object", name="data") | ||
s2.loc[:] = s1 | ||
tm.assert_numpy_array_equal(np.array(s1), np.array(s2)) | ||
assert s2.dtype == np.object | ||
tm.assert_series_equal(s1, s2) | ||
|
||
|
||
def test_loc_setitem_series_timedelta64_full_with_index(): | ||
s1 = pd.Series(pd.timedelta_range(start="1 day", periods=4), name="data") | ||
s2 = pd.Series(index=s1.index, dtype="object", name="data") | ||
s2.loc[s1.index] = s1 | ||
tm.assert_series_equal(s1, s2) | ||
|
||
|
||
def test_loc_setitem_df_period_full_column_with_index(): | ||
df = pd.DataFrame(pd.period_range(start="2020Q1", periods=5), columns=["data"]) | ||
df2 = pd.DataFrame(index=df.index) | ||
df2.loc[df.index, "data"] = df["data"] | ||
tm.assert_frame_equal(df, df2) | ||
|
||
|
||
def test_loc_setitem_series_interval_full_with_index(): | ||
s1 = pd.Series(pd.interval_range(start=0, end=5), name="data") | ||
s2 = pd.Series(index=s1.index, dtype="object", name="data") | ||
s2.loc[s1.index] = s1 | ||
tm.assert_series_equal(s1, s2) | ||
|
||
|
||
def test_loc_setitem_df_sparse_full_column_with_index(): | ||
df = pd.DataFrame(np.random.randn(100), columns=["data"]) | ||
df.iloc[5:95] = np.nan | ||
df = df.astype(pd.SparseDtype("int", np.nan)) | ||
df2 = pd.DataFrame(index=df.index) | ||
df2.loc[df.index, "data"] = df["data"] | ||
tm.assert_frame_equal(df, df2) | ||
|
||
|
||
def test_loc_setitem_series_categorical_full_with_index(): | ||
s1 = pd.Series(pd.Categorical([1] * 10 + [2] * 5 + [3] * 10), name="data") | ||
s2 = pd.Series(index=s1.index, dtype="object", name="data") | ||
s2.loc[s1.index] = s1 | ||
tm.assert_series_equal(s1, s2) | ||
|
||
|
||
def test_loc_setitem_series_categorical_slice(): | ||
s1 = pd.Series(pd.Categorical([1] * 10 + [2] * 5 + [3] * 10), name="data") | ||
s2 = 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 | ||
) | ||
s2.loc[s1.index[:15]] = s1[:15] | ||
tm.assert_series_equal(expected, s2) | ||
|
||
|
||
def test_loc_setitem_df_interval_slice(): | ||
df = pd.DataFrame(pd.interval_range(start=0, end=5), columns=["data"]) | ||
df2 = pd.DataFrame(index=df.index) | ||
expected = pd.DataFrame( | ||
[*pd.interval_range(start=0, end=3), np.nan, np.nan], columns=["data"] | ||
) | ||
df2.loc[df.index[:3], "data"] = df["data"][:3] | ||
tm.assert_frame_equal(df2, expected) | ||
|
||
|
||
def test_loc_setitem_series_period_slice(): | ||
s1 = pd.Series(pd.period_range(start="2020Q1", periods=5), name="data") | ||
s2 = pd.Series(index=s1.index, dtype="object", name="data") | ||
expected = pd.Series( | ||
[*pd.period_range(start="2020Q1", periods=3), np.nan, np.nan], | ||
name="data", | ||
dtype=object, | ||
) | ||
s2.loc[s1.index[:3]] = s1[:3] | ||
tm.assert_series_equal(expected, s2) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
revert this