-
-
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
Conversation
@@ -4441,6 +4441,15 @@ def test_nat_operations(self): | |||
self.assertEqual(s.min(), exp) | |||
self.assertEqual(s.max(), exp) | |||
|
|||
def test_round_nat(self): |
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 ops
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.
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
In [1]: pd.Timestamp('NaT')
Out[1]: NaT
In [2]: pd.Timestamp('NaT').round('s')
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-2-05da84ef741e> in <module>()
----> 1 pd.Timestamp('NaT').round('s')
AttributeError: 'NaTType' object has no attribute 'round'
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:
dt = pd.DatetimeIndex([pd.Timestamp('nat')])
for method in ["round", "floor", "ceil"]:
round_method = getattr(dt, method)
for freq in ["s", "5s", "min", "5min", "h", "5h"]:
tm.assert_index_equal(round_method(freq), dt)
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
ts = pd.Timestamp('nat')
for method in [....]:
round_method = ....
for freq in [.....]:
self.assertEqual(round_method(freq), ts)
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 the DatetimeProperties
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
5b31e77
to
9e77191
Compare
_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 comment
The 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
@@ -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 comment
The 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 comment
The reason will be displayed to describe this comment to others. Learn more.
@jreback NaTType
class doesn't has a round
method. It would unnecessary if I'll define round
abstract method at the _Timestamp
class.
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 comment
The 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 comment
The reason will be displayed to describe this comment to others. Learn more.
move this test right after test_round
# 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 comment
The reason will be displayed to describe this comment to others. Learn more.
move this tests right after test_round
thanks! |
…erent arguments closes pandas-dev#14940 Author: discort <[email protected]> Closes pandas-dev#15124 from discort/dt_round_bug_14940 and squashes the following commits: 9e77191 [discort] added a test for Timestamp 52c897a [discort] BUG: added maybe_mask_results to '_round' method
git diff upstream/master | flake8 --diff