Skip to content

Commit f3c5a42

Browse files
opensourceworkARjreback
authored andcommitted
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
1 parent e503d40 commit f3c5a42

File tree

4 files changed

+28
-10
lines changed

4 files changed

+28
-10
lines changed

doc/source/whatsnew/v0.19.2.txt

+1
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ Bug Fixes
4747
- 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`)
4848
- 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.
4949
- 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`)
50+
- Bug in ``.fillna()`` in which timezone aware datetime64 values were incorrectly rounded (:issue:`14872`)
5051

5152

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

pandas/core/missing.py

+6-7
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
needs_i8_conversion)
1716
from pandas.types.missing import isnull
1817

@@ -450,7 +449,7 @@ def pad_1d(values, limit=None, mask=None, dtype=None):
450449
_method = None
451450
if is_float_dtype(values):
452451
_method = getattr(algos, 'pad_inplace_%s' % dtype.name, None)
453-
elif dtype in _DATELIKE_DTYPES or is_datetime64_dtype(values):
452+
elif is_datetime64_dtype(dtype) or is_datetime64tz_dtype(dtype):
454453
_method = _pad_1d_datetime
455454
elif is_integer_dtype(values):
456455
values = _ensure_float64(values)
@@ -475,7 +474,7 @@ def backfill_1d(values, limit=None, mask=None, dtype=None):
475474
_method = None
476475
if is_float_dtype(values):
477476
_method = getattr(algos, 'backfill_inplace_%s' % dtype.name, None)
478-
elif dtype in _DATELIKE_DTYPES or is_datetime64_dtype(values):
477+
elif is_datetime64_dtype(dtype) or is_datetime64tz_dtype(dtype):
479478
_method = _backfill_1d_datetime
480479
elif is_integer_dtype(values):
481480
values = _ensure_float64(values)
@@ -501,7 +500,7 @@ def pad_2d(values, limit=None, mask=None, dtype=None):
501500
_method = None
502501
if is_float_dtype(values):
503502
_method = getattr(algos, 'pad_2d_inplace_%s' % dtype.name, None)
504-
elif dtype in _DATELIKE_DTYPES or is_datetime64_dtype(values):
503+
elif is_datetime64_dtype(dtype) or is_datetime64tz_dtype(dtype):
505504
_method = _pad_2d_datetime
506505
elif is_integer_dtype(values):
507506
values = _ensure_float64(values)
@@ -531,7 +530,7 @@ def backfill_2d(values, limit=None, mask=None, dtype=None):
531530
_method = None
532531
if is_float_dtype(values):
533532
_method = getattr(algos, 'backfill_2d_inplace_%s' % dtype.name, None)
534-
elif dtype in _DATELIKE_DTYPES or is_datetime64_dtype(values):
533+
elif is_datetime64_dtype(dtype) or is_datetime64tz_dtype(dtype):
535534
_method = _backfill_2d_datetime
536535
elif is_integer_dtype(values):
537536
values = _ensure_float64(values)

pandas/tests/series/test_missing.py

+20-3
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)
@@ -908,7 +926,6 @@ def test_interp_timedelta64(self):
908926
index=pd.to_timedelta([1, 2, 4]))
909927
assert_series_equal(result, expected)
910928

911-
912929
if __name__ == '__main__':
913930
import nose
914931
nose.runmodule(argv=[__file__, '-vvs', '-x', '--pdb', '--pdb-failure'],

pandas/types/common.py

+1
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)