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
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions doc/source/whatsnew/v0.20.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -366,3 +366,5 @@ Bug Fixes


- Bug in ``pd.read_csv()`` for the C engine where ``usecols`` were being indexed incorrectly with ``parse_dates`` (:issue:`14792`)

- Bug in ``Series.dt.round`` inconsistent behaviour on NAT's with different arguments (:issue:`14940`)
1 change: 1 addition & 0 deletions pandas/tseries/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ def _round(self, freq, rounder):
values = _ensure_datetimelike_to_i8(self)

result = (unit * rounder(values / float(unit))).astype('i8')
result = self._maybe_mask_results(result, fill_value=tslib.NaT)
attribs = self._get_attributes_dict()
if 'freq' in attribs:
attribs['freq'] = None
Expand Down
18 changes: 18 additions & 0 deletions pandas/tseries/tests/test_timeseries.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

# 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

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):
Expand Down Expand Up @@ -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))
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

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):
Expand Down
7 changes: 6 additions & 1 deletion pandas/tslib.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -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)
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


_nan_methods = ['weekday', 'isoweekday', 'total_seconds']

Expand All @@ -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.

setattr(NaTType, _method_name, _make_nat_func(_method_name))

for _method_name in _nan_methods:
Expand Down