-
-
Notifications
You must be signed in to change notification settings - Fork 18.6k
Assignment of column via .loc for numpy non-ns datetimes #27928
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 all commits
ed9d7f0
49471e7
9efb169
41df89d
6df540c
98a5061
299a5b1
cf18fb9
09ab203
93c2f85
ad771f5
35009b7
6a48ca6
063cc37
d64a2d8
86fd1c1
2ddbc01
48e454a
c8eb91f
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 |
---|---|---|
|
@@ -52,7 +52,6 @@ Indexing | |
- | ||
- | ||
- | ||
- | ||
|
||
Missing | ||
^^^^^^^ | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -690,6 +690,28 @@ def test_loc_setitem_consistency_slice_column_len(self): | |
) | ||
tm.assert_series_equal(df[("Respondent", "Duration")], expected) | ||
|
||
@pytest.mark.parametrize("unit", ["Y", "M", "D", "h", "m", "s", "ms", "us"]) | ||
def test_loc_assign_non_ns_datetime(self, unit): | ||
# GH 27395, non-ns dtype assignment via .loc should work | ||
# and return the same result when using simple assignment | ||
df = DataFrame( | ||
{ | ||
"timestamp": [ | ||
np.datetime64("2017-02-11 12:41:29"), | ||
np.datetime64("1991-11-07 04:22:37"), | ||
] | ||
} | ||
) | ||
|
||
df.loc[:, unit] = df.loc[:, "timestamp"].values.astype( | ||
"datetime64[{unit}]".format(unit=unit) | ||
) | ||
df["expected"] = df.loc[:, "timestamp"].values.astype( | ||
"datetime64[{unit}]".format(unit=unit) | ||
) | ||
expected = Series(df.loc[:, "expected"], name=unit) | ||
tm.assert_series_equal(df.loc[:, unit], expected) | ||
|
||
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. Can you add an assert about the result here? 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. While I update the tests, I found a strange result. (Not sure this is intended.) df = pd.DataFrame({'timestamp': [np.datetime64('2017-01-01 01:02:23'), np.datetime64('2018-11-12 12:34:12')]})
df['year'] = df.loc[:, 'timestamp'].values.astype('datetime64[Y]')
df['month'] = df.loc[:, 'timestamp'].values.astype('datetime64[M]')
df.loc[:, 'day'] = df.loc[:, 'timestamp'].values.astype('datetime64[D]') result:
df.loc[:, 'timestamp'].values.astype('datetime64[Y]') result:
If this is intended, then I'll make an assert with this result and remove previous test (pandas/tests/dtypes/cast/test_infer_dtype.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. Without running the test in master, it isn't obvious to the reader what is being tested. I suggest something like:
|
||
def test_loc_setitem_frame(self): | ||
df = self.frame_labels | ||
|
||
|
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.
What does
<=
do for numpy dtypes? Check if it's a subtype?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.
Not clear to me either:
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.
@TomAugspurger @jbrockmendel
It checks whether the dtype timespan is longer than
[ns]
or shorter. (FYI, https://docs.scipy.org/doc/numpy/reference/arrays.datetime.html#datetime-units)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.
can you add a comment to this effect (in both places)