Skip to content

Commit

Permalink
BUG: pandas.Series.dt.round inconsistent behaviour on NAT's with diff…
Browse files Browse the repository at this point in the history
…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
  • Loading branch information
discort authored and jreback committed Jan 14, 2017
1 parent 7ad6c65 commit 2c81129
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 1 deletion.
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 @@ -368,3 +368,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 @@ -4453,6 +4453,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"]:
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 @@ -4861,6 +4870,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))
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)

_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:
setattr(NaTType, _method_name, _make_nat_func(_method_name))

for _method_name in _nan_methods:
Expand Down

0 comments on commit 2c81129

Please sign in to comment.