Skip to content

PR for Pandas issue #14872 / fillna() TZ datetime64 rounded #14905

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 10 commits into from
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.19.2.txt
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ Other Enhancements
Bug Fixes
~~~~~~~~~

- Bug in fillna() in which timezone aware datetime64 values were incorrectly rounded (:issue:'14872')
- Compat with ``dateutil==2.6.0``; segfault reported in the testing suite (:issue:`14621`)
- Allow ``nanoseconds`` in ``Timestamp.replace`` as a kwarg (:issue:`14621`)
- Bug in ``pd.read_csv`` in which aliasing was being done for ``na_values`` when passed in as a dictionary (:issue:`14203`)
Expand Down
13 changes: 6 additions & 7 deletions pandas/core/missing.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@
from pandas.compat import range, string_types
from pandas.types.common import (is_numeric_v_string_like,
is_float_dtype, is_datetime64_dtype,
is_integer_dtype, _ensure_float64,
is_scalar,
_DATELIKE_DTYPES,
is_datetime64tz_dtype, is_integer_dtype,
_ensure_float64, is_scalar,
needs_i8_conversion)
from pandas.types.missing import isnull

Expand Down Expand Up @@ -450,7 +449,7 @@ def pad_1d(values, limit=None, mask=None, dtype=None):
_method = None
if is_float_dtype(values):
_method = getattr(algos, 'pad_inplace_%s' % dtype.name, None)
elif dtype in _DATELIKE_DTYPES or is_datetime64_dtype(values):
elif is_datetime64_dtype(dtype) or is_datetime64tz_dtype(dtype):
_method = _pad_1d_datetime
elif is_integer_dtype(values):
values = _ensure_float64(values)
Expand All @@ -475,7 +474,7 @@ def backfill_1d(values, limit=None, mask=None, dtype=None):
_method = None
if is_float_dtype(values):
_method = getattr(algos, 'backfill_inplace_%s' % dtype.name, None)
elif dtype in _DATELIKE_DTYPES or is_datetime64_dtype(values):
elif is_datetime64_dtype(dtype) or is_datetime64tz_dtype(dtype):
_method = _backfill_1d_datetime
elif is_integer_dtype(values):
values = _ensure_float64(values)
Expand All @@ -501,7 +500,7 @@ def pad_2d(values, limit=None, mask=None, dtype=None):
_method = None
if is_float_dtype(values):
_method = getattr(algos, 'pad_2d_inplace_%s' % dtype.name, None)
elif dtype in _DATELIKE_DTYPES or is_datetime64_dtype(values):
elif is_datetime64_dtype(dtype) or is_datetime64tz_dtype(dtype):
_method = _pad_2d_datetime
elif is_integer_dtype(values):
values = _ensure_float64(values)
Expand Down Expand Up @@ -531,7 +530,7 @@ def backfill_2d(values, limit=None, mask=None, dtype=None):
_method = None
if is_float_dtype(values):
_method = getattr(algos, 'backfill_2d_inplace_%s' % dtype.name, None)
elif dtype in _DATELIKE_DTYPES or is_datetime64_dtype(values):
elif is_datetime64_dtype(dtype) or is_datetime64tz_dtype(dtype):
_method = _backfill_2d_datetime
elif is_integer_dtype(values):
values = _ensure_float64(values)
Expand Down
21 changes: 21 additions & 0 deletions pandas/tests/series/test_missing.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@

from .common import TestData

import datetime
import pytz


def _skip_if_no_pchip():
try:
Expand Down Expand Up @@ -908,6 +911,24 @@ def test_interp_timedelta64(self):
index=pd.to_timedelta([1, 2, 4]))
assert_series_equal(result, expected)

# GH 14872
def test_dtype_utc():
Copy link
Contributor

Choose a reason for hiding this comment

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

needs to take self


data = pd.Series([pd.NaT, pd.NaT,
datetime.datetime(2016, 12, 12, 22, 24, 6, 100001,
tzinfo=pytz.utc)])

filled = data.fillna(method='bfill')

expected = pd.Series([
datetime.datetime(2016, 12, 12, 22, 24, 6,
100001, tzinfo=pytz.utc),
datetime.datetime(2016, 12, 12, 22, 24, 6,
100001, tzinfo=pytz.utc),
datetime.datetime(2016, 12, 12, 22, 24, 6,
100001, tzinfo=pytz.utc)])

assert_series_equal(filled, expected)

if __name__ == '__main__':
import nose
Expand Down
1 change: 1 addition & 0 deletions pandas/types/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
_NS_DTYPE = np.dtype('M8[ns]')
_TD_DTYPE = np.dtype('m8[ns]')
_INT64_DTYPE = np.dtype(np.int64)

_DATELIKE_DTYPES = set([np.dtype(t)
for t in ['M8[ns]', '<M8[ns]', '>M8[ns]',
'm8[ns]', '<m8[ns]', '>m8[ns]']])
Expand Down