Skip to content

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

Closed
wants to merge 2 commits into from

Conversation

discort
Copy link
Contributor

@discort discort commented Jan 13, 2017

@@ -4441,6 +4441,15 @@ def test_nat_operations(self):
self.assertEqual(s.min(), exp)
self.assertEqual(s.max(), exp)

def test_round_nat(self):
Copy link
Contributor

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

Copy link
Contributor Author

@discort discort Jan 13, 2017

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?

Copy link
Contributor

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!

Copy link
Contributor Author

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?

Copy link
Contributor

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)

Copy link
Contributor Author

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?

Copy link
Contributor

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

Copy link
Contributor Author

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

@jreback jreback added Bug Missing-data np.nan, pd.NaT, pd.NA, dropna, isnull, interpolate Datetime Datetime data dtype labels Jan 13, 2017
@discort discort force-pushed the dt_round_bug_14940 branch from 5b31e77 to 9e77191 Compare January 13, 2017 22:43
_nat_methods = ['date', 'now', 'replace', 'to_pydatetime', 'today']
_nat_methods.extend(_round_methods)
Copy link
Contributor

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:
Copy link
Contributor

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

Copy link
Contributor Author

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))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

extra print

Copy link
Contributor

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"]:
Copy link
Contributor

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

@jreback jreback added this to the 0.20.0 milestone Jan 14, 2017
@jreback jreback closed this in 2c81129 Jan 14, 2017
@jreback
Copy link
Contributor

jreback commented Jan 14, 2017

thanks!

AnkurDedania pushed a commit to AnkurDedania/pandas that referenced this pull request Mar 21, 2017
…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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Datetime Datetime data dtype Missing-data np.nan, pd.NaT, pd.NA, dropna, isnull, interpolate
Projects
None yet
Development

Successfully merging this pull request may close these issues.

BUG: pandas.Series.dt.round inconsistent behaviour on NAT's with different arguments?
2 participants