Skip to content

Commit bbb7686

Browse files
BUG: .fillna() for datetime64 with tz is passing thru floats
closes pandas-dev#14872 Author: Rodolfo Fernandez <[email protected]> Closes pandas-dev#14905 from RodolfoRFR/pandas-14872-e and squashes the following commits: 18802b4 [Rodolfo Fernandez] added 'self' to test_dtype_utc function in pandas/tests/series/test_missing e0c6c7c [Rodolfo Fernandez] added line to whatsnew v0.19.2 and test to test_missing.py in series folder e4ba7e0 [Rodolfo Fernandez] removed all references to _DATELIKE_DTYPES from /pandas/core/missing.py 5d37ce8 [Rodolfo Fernandez] added is_datetime64tz_dtype and changed evaluation from 'values' to dtype 19eecb2 [Rodolfo Fernandez] fixed style errors using flake8 59b91a1 [Rodolfo Fernandez] test modified 5a59eac [Rodolfo Fernandez] test modified bc68bf7 [Rodolfo Fernandez] test modified ba83fc8 [Rodolfo Fernandez] test b7358de [Rodolfo Fernandez] bug fixed (cherry picked from commit f3c5a42)
1 parent c9e5bf4 commit bbb7686

File tree

4 files changed

+45
-9
lines changed

4 files changed

+45
-9
lines changed

doc/source/whatsnew/v0.19.2.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ Bug Fixes
4646
- Bug in ``pd.read_csv`` for the Python engine in which an unhelpful error message was being raised when multi-char delimiters were not being respected with quotes (:issue:`14582`)
4747
- Fix bugs (:issue:`14734`, :issue:`13654`) in ``pd.read_sas`` and ``pandas.io.sas.sas7bdat.SAS7BDATReader`` that caused problems when reading a SAS file incrementally.
4848
- Bug in ``pd.read_csv`` for the Python engine in which an unhelpful error message was being raised when ``skipfooter`` was not being respected by Python's CSV library (:issue:`13879`)
49+
- Bug in ``.fillna()`` in which timezone aware datetime64 values were incorrectly rounded (:issue:`14872`)
4950

5051

5152
- Bug in ``.groupby(..., sort=True)`` of a non-lexsorted MultiIndex when grouping with multiple levels (:issue:`14776`)

pandas/core/missing.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,8 @@
1010
from pandas.compat import range, string_types
1111
from pandas.types.common import (is_numeric_v_string_like,
1212
is_float_dtype, is_datetime64_dtype,
13-
is_integer_dtype, _ensure_float64,
14-
is_scalar,
15-
_DATELIKE_DTYPES)
13+
is_datetime64tz_dtype, is_integer_dtype,
14+
_ensure_float64, is_scalar)
1615
from pandas.types.missing import isnull
1716

1817

@@ -449,7 +448,7 @@ def pad_1d(values, limit=None, mask=None, dtype=None):
449448
_method = None
450449
if is_float_dtype(values):
451450
_method = getattr(algos, 'pad_inplace_%s' % dtype.name, None)
452-
elif dtype in _DATELIKE_DTYPES or is_datetime64_dtype(values):
451+
elif is_datetime64_dtype(dtype) or is_datetime64tz_dtype(dtype):
453452
_method = _pad_1d_datetime
454453
elif is_integer_dtype(values):
455454
values = _ensure_float64(values)
@@ -474,7 +473,7 @@ def backfill_1d(values, limit=None, mask=None, dtype=None):
474473
_method = None
475474
if is_float_dtype(values):
476475
_method = getattr(algos, 'backfill_inplace_%s' % dtype.name, None)
477-
elif dtype in _DATELIKE_DTYPES or is_datetime64_dtype(values):
476+
elif is_datetime64_dtype(dtype) or is_datetime64tz_dtype(dtype):
478477
_method = _backfill_1d_datetime
479478
elif is_integer_dtype(values):
480479
values = _ensure_float64(values)
@@ -500,7 +499,7 @@ def pad_2d(values, limit=None, mask=None, dtype=None):
500499
_method = None
501500
if is_float_dtype(values):
502501
_method = getattr(algos, 'pad_2d_inplace_%s' % dtype.name, None)
503-
elif dtype in _DATELIKE_DTYPES or is_datetime64_dtype(values):
502+
elif is_datetime64_dtype(dtype) or is_datetime64tz_dtype(dtype):
504503
_method = _pad_2d_datetime
505504
elif is_integer_dtype(values):
506505
values = _ensure_float64(values)
@@ -530,7 +529,7 @@ def backfill_2d(values, limit=None, mask=None, dtype=None):
530529
_method = None
531530
if is_float_dtype(values):
532531
_method = getattr(algos, 'backfill_2d_inplace_%s' % dtype.name, None)
533-
elif dtype in _DATELIKE_DTYPES or is_datetime64_dtype(values):
532+
elif is_datetime64_dtype(dtype) or is_datetime64tz_dtype(dtype):
534533
_method = _backfill_2d_datetime
535534
elif is_integer_dtype(values):
536535
values = _ensure_float64(values)

pandas/tests/series/test_missing.py

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
# coding=utf-8
22
# pylint: disable-msg=E1101,W0612
33

4-
from datetime import timedelta
4+
import pytz
5+
from datetime import timedelta, datetime
56

67
from numpy import nan
78
import numpy as np
@@ -10,7 +11,6 @@
1011
from pandas import (Series, isnull, date_range,
1112
MultiIndex, Index)
1213
from pandas.tseries.index import Timestamp
13-
1414
from pandas.compat import range
1515
from pandas.util.testing import assert_series_equal
1616
import pandas.util.testing as tm
@@ -250,6 +250,24 @@ def test_datetime64_tz_fillna(self):
250250
self.assert_series_equal(expected, result)
251251
self.assert_series_equal(pd.isnull(s), null_loc)
252252

253+
def test_datetime64tz_fillna_round_issue(self):
254+
# GH 14872
255+
256+
data = pd.Series([pd.NaT, pd.NaT,
257+
datetime(2016, 12, 12, 22, 24, 6, 100001,
258+
tzinfo=pytz.utc)])
259+
260+
filled = data.fillna(method='bfill')
261+
262+
expected = pd.Series([datetime(2016, 12, 12, 22, 24, 6,
263+
100001, tzinfo=pytz.utc),
264+
datetime(2016, 12, 12, 22, 24, 6,
265+
100001, tzinfo=pytz.utc),
266+
datetime(2016, 12, 12, 22, 24, 6,
267+
100001, tzinfo=pytz.utc)])
268+
269+
assert_series_equal(filled, expected)
270+
253271
def test_fillna_int(self):
254272
s = Series(np.random.randint(-100, 100, 50))
255273
s.fillna(method='ffill', inplace=True)
@@ -891,6 +909,23 @@ def test_spline_error(self):
891909
with tm.assertRaises(ValueError):
892910
s.interpolate(method='spline', order=0)
893911

912+
def test_interp_timedelta64(self):
913+
# GH 6424
914+
df = Series([1, np.nan, 3],
915+
index=pd.to_timedelta([1, 2, 3]))
916+
result = df.interpolate(method='time')
917+
expected = Series([1., 2., 3.],
918+
index=pd.to_timedelta([1, 2, 3]))
919+
assert_series_equal(result, expected)
920+
921+
# test for non uniform spacing
922+
df = Series([1, np.nan, 3],
923+
index=pd.to_timedelta([1, 2, 4]))
924+
result = df.interpolate(method='time')
925+
expected = Series([1., 1.666667, 3.],
926+
index=pd.to_timedelta([1, 2, 4]))
927+
assert_series_equal(result, expected)
928+
894929

895930
if __name__ == '__main__':
896931
import nose

pandas/types/common.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
_NS_DTYPE = np.dtype('M8[ns]')
2323
_TD_DTYPE = np.dtype('m8[ns]')
2424
_INT64_DTYPE = np.dtype(np.int64)
25+
2526
_DATELIKE_DTYPES = set([np.dtype(t)
2627
for t in ['M8[ns]', '<M8[ns]', '>M8[ns]',
2728
'm8[ns]', '<m8[ns]', '>m8[ns]']])

0 commit comments

Comments
 (0)