Skip to content

Commit df791a7

Browse files
committed
BUG/ENH: add bfill/ffill support for datetime64[ns]
1 parent 36d49a9 commit df791a7

File tree

3 files changed

+38
-2
lines changed

3 files changed

+38
-2
lines changed

doc/source/release.rst

+2-1
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,9 @@ pandas 0.13
6767
- A Series of dtype ``timedelta64[ns]`` can now be divided by another
6868
``timedelta64[ns]`` object to yield a ``float64`` dtyped Series. This
6969
is frequency conversion.
70-
- Timedeltas support ``fillna`` with an integer interpreted as seconds,
70+
- Timedelta64 support ``fillna/ffill/bfill`` with an integer interpreted as seconds,
7171
or a ``timedelta`` (:issue:`3371`)
72+
- Datetime64 support ``ffill/bfill``
7273
- Performance improvements with ``__getitem__`` on ``DataFrames`` with
7374
when the key is a column
7475
- Support for using a ``DatetimeIndex/PeriodsIndex`` directly in a datelike calculation

pandas/core/internals.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -1043,7 +1043,8 @@ def _try_operate(self, values):
10431043
def _try_coerce_result(self, result):
10441044
""" reverse of try_coerce_args / try_operate """
10451045
if isinstance(result, np.ndarray):
1046-
result = result.astype('m8[ns]')
1046+
if result.dtype.kind in ['i','f','O']:
1047+
result = result.astype('m8[ns]')
10471048
elif isinstance(result, np.integer):
10481049
result = np.timedelta64(result)
10491050
return result
@@ -1299,6 +1300,8 @@ def _try_coerce_result(self, result):
12991300
if result.dtype == 'i8':
13001301
result = tslib.array_to_datetime(
13011302
result.astype(object).ravel()).reshape(result.shape)
1303+
elif result.dtype.kind in ['i','f','O']:
1304+
result = result.astype('M8[ns]')
13021305
elif isinstance(result, (np.integer, np.datetime64)):
13031306
result = lib.Timestamp(result)
13041307
return result

pandas/tests/test_series.py

+32
Original file line numberDiff line numberDiff line change
@@ -2445,6 +2445,38 @@ def test_timedelta_fillna(self):
24452445
expected[0] = np.nan
24462446
assert_series_equal(result,expected)
24472447

2448+
# bfill
2449+
td[2] = np.nan
2450+
result = td.bfill()
2451+
expected = td.fillna(0)
2452+
expected[2] = timedelta(days=1,seconds=9*3600+60+1)
2453+
assert_series_equal(result,expected)
2454+
2455+
def test_datetime64_fillna(self):
2456+
2457+
s = Series([Timestamp('20130101'),Timestamp('20130101'),Timestamp('20130102'),Timestamp('20130103 9:01:01')])
2458+
s[2] = np.nan
2459+
2460+
# reg fillna
2461+
result = s.fillna(Timestamp('20130104'))
2462+
expected = Series([Timestamp('20130101'),Timestamp('20130101'),Timestamp('20130104'),Timestamp('20130103 9:01:01')])
2463+
assert_series_equal(result,expected)
2464+
2465+
from pandas import tslib
2466+
result = s.fillna(tslib.NaT)
2467+
expected = s
2468+
assert_series_equal(result,expected)
2469+
2470+
# ffill
2471+
result = s.ffill()
2472+
expected = Series([Timestamp('20130101'),Timestamp('20130101'),Timestamp('20130101'),Timestamp('20130103 9:01:01')])
2473+
assert_series_equal(result,expected)
2474+
2475+
# bfill
2476+
result = s.bfill()
2477+
expected = Series([Timestamp('20130101'),Timestamp('20130101'),Timestamp('20130103 9:01:01'),Timestamp('20130103 9:01:01')])
2478+
assert_series_equal(result,expected)
2479+
24482480
def test_sub_of_datetime_from_TimeSeries(self):
24492481
from pandas.core import common as com
24502482
from datetime import datetime

0 commit comments

Comments
 (0)