-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
BUG: pandas.Series.dt.round inconsistent behaviour on NAT's with different arguments #15124
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
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 |
---|---|---|
|
@@ -4441,6 +4441,15 @@ def test_nat_operations(self): | |
self.assertEqual(s.min(), exp) | ||
self.assertEqual(s.max(), exp) | ||
|
||
def test_round_nat(self): | ||
# GH14940 | ||
s = Series([pd.NaT]) | ||
expected = Series(pd.NaT) | ||
for method in ["round", "floor", "ceil"]: | ||
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. move this tests right after test_round |
||
round_method = getattr(s.dt, method) | ||
for freq in ["s", "5s", "min", "5min", "h", "5h"]: | ||
assert_series_equal(round_method(freq), expected) | ||
|
||
|
||
class TestTimestamp(tm.TestCase): | ||
def test_class_ops_pytz(self): | ||
|
@@ -4849,6 +4858,15 @@ def test_is_leap_year(self): | |
self.assertFalse(pd.NaT.is_leap_year) | ||
self.assertIsInstance(pd.NaT.is_leap_year, bool) | ||
|
||
def test_round_nat(self): | ||
# GH14940 | ||
ts = Timestamp('nat') | ||
print(dir(ts)) | ||
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. extra print 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. move this test right after test_round |
||
for method in ["round", "floor", "ceil"]: | ||
round_method = getattr(ts, method) | ||
for freq in ["s", "5s", "min", "5min", "h", "5h"]: | ||
self.assertIs(round_method(freq), ts) | ||
|
||
|
||
class TestSlicing(tm.TestCase): | ||
def test_slice_year(self): | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -885,7 +885,12 @@ def _make_nan_func(func_name): | |
f.__name__ = func_name | ||
return f | ||
|
||
|
||
# GH14940 | ||
_round_methods = ['round', 'floor', 'ceil'] | ||
|
||
_nat_methods = ['date', 'now', 'replace', 'to_pydatetime', 'today'] | ||
_nat_methods.extend(_round_methods) | ||
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 can just directly add these I think, no need to have a separate _round_methods |
||
|
||
_nan_methods = ['weekday', 'isoweekday', 'total_seconds'] | ||
|
||
|
@@ -895,7 +900,7 @@ _implemented_methods.extend(_nan_methods) | |
|
||
for _method_name in _nat_methods: | ||
# not all methods exist in all versions of Python | ||
if hasattr(NaTType, _method_name): | ||
if hasattr(NaTType, _method_name) or _method_name in _round_methods: | ||
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 don't think this is necessary 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 |
||
setattr(NaTType, _method_name, _make_nat_func(_method_name)) | ||
|
||
for _method_name in _nan_methods: | ||
|
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.
add a test in the class below for
Timestamp('nat')
with the same opsThere 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.
Does exist a way to check a message of exception? Or should I check only for
AttributeError
?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.
no this should work, on a Timestamp (it will just return the same). so no need to check for an exception. Your test should fail w/o a fix though
and of course the fix is to define these!
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.
I suppose you mean something like that:
Could you please confirm?
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.
no, that should be convered by your previous test I mean
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.
I'm sorry, Jeff. I didn't get what do you mean. In the previous test, I tested
round
,ceil
,floor
methods of theDatetimeProperties
obj.Could you please clarify for what object should I test the methods?
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.
no you tested
DatetimeIndex
, I also want the scalar tested,Timestamp
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.
Thank you. Added a test