Skip to content

ENH: GH3371 support timedelta fillna #4684

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

Merged
merged 3 commits into from
Sep 8, 2013
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
BUG/ENH: add bfill/ffill support for datetime64[ns]
  • Loading branch information
jreback committed Sep 7, 2013
commit df791a7fa9cef30d0e1d8b15ddb8843afa77f409
3 changes: 2 additions & 1 deletion doc/source/release.rst
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,9 @@ pandas 0.13
- A Series of dtype ``timedelta64[ns]`` can now be divided by another
``timedelta64[ns]`` object to yield a ``float64`` dtyped Series. This
is frequency conversion.
- Timedeltas support ``fillna`` with an integer interpreted as seconds,
- Timedelta64 support ``fillna/ffill/bfill`` with an integer interpreted as seconds,
or a ``timedelta`` (:issue:`3371`)
- Datetime64 support ``ffill/bfill``
- Performance improvements with ``__getitem__`` on ``DataFrames`` with
when the key is a column
- Support for using a ``DatetimeIndex/PeriodsIndex`` directly in a datelike calculation
Expand Down
5 changes: 4 additions & 1 deletion pandas/core/internals.py
Original file line number Diff line number Diff line change
Expand Up @@ -1043,7 +1043,8 @@ def _try_operate(self, values):
def _try_coerce_result(self, result):
""" reverse of try_coerce_args / try_operate """
if isinstance(result, np.ndarray):
result = result.astype('m8[ns]')
if result.dtype.kind in ['i','f','O']:
result = result.astype('m8[ns]')
elif isinstance(result, np.integer):
result = np.timedelta64(result)
return result
Expand Down Expand Up @@ -1299,6 +1300,8 @@ def _try_coerce_result(self, result):
if result.dtype == 'i8':
result = tslib.array_to_datetime(
result.astype(object).ravel()).reshape(result.shape)
elif result.dtype.kind in ['i','f','O']:
result = result.astype('M8[ns]')
elif isinstance(result, (np.integer, np.datetime64)):
result = lib.Timestamp(result)
return result
Expand Down
32 changes: 32 additions & 0 deletions pandas/tests/test_series.py
Original file line number Diff line number Diff line change
Expand Up @@ -2445,6 +2445,38 @@ def test_timedelta_fillna(self):
expected[0] = np.nan
assert_series_equal(result,expected)

# bfill
td[2] = np.nan
result = td.bfill()
expected = td.fillna(0)
expected[2] = timedelta(days=1,seconds=9*3600+60+1)
assert_series_equal(result,expected)

def test_datetime64_fillna(self):

s = Series([Timestamp('20130101'),Timestamp('20130101'),Timestamp('20130102'),Timestamp('20130103 9:01:01')])
s[2] = np.nan

# reg fillna
result = s.fillna(Timestamp('20130104'))
expected = Series([Timestamp('20130101'),Timestamp('20130101'),Timestamp('20130104'),Timestamp('20130103 9:01:01')])
assert_series_equal(result,expected)

from pandas import tslib
result = s.fillna(tslib.NaT)
expected = s
assert_series_equal(result,expected)

# ffill
result = s.ffill()
expected = Series([Timestamp('20130101'),Timestamp('20130101'),Timestamp('20130101'),Timestamp('20130103 9:01:01')])
assert_series_equal(result,expected)

# bfill
result = s.bfill()
expected = Series([Timestamp('20130101'),Timestamp('20130101'),Timestamp('20130103 9:01:01'),Timestamp('20130103 9:01:01')])
assert_series_equal(result,expected)

def test_sub_of_datetime_from_TimeSeries(self):
from pandas.core import common as com
from datetime import datetime
Expand Down