Skip to content

BUG: to_timedelta of a scalar returns a scalar, closes #5410. #5415

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 1 commit into from
Nov 4, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions doc/source/release.rst
Original file line number Diff line number Diff line change
Expand Up @@ -770,6 +770,7 @@ Bug Fixes
and DataFrames that have repeated (non-unique) indices. (:issue:`4620`)
- Fix empty series not printing name in repr (:issue:`4651`)
- Make tests create temp files in temp directory by default. (:issue:`5419`)
- ``pd.to_timedelta`` of a scalar returns a scalar (:issue:`5410`)

pandas 0.12.0
-------------
Expand Down
19 changes: 19 additions & 0 deletions pandas/tests/test_series.py
Original file line number Diff line number Diff line change
Expand Up @@ -2314,6 +2314,25 @@ def test_timedelta64_operations_with_timedeltas(self):
# roundtrip
assert_series_equal(result + td2,td1)

# Now again, using pd.to_timedelta, which should build
# a Series or a scalar, depending on input.
if not _np_version_under1p7:
td1 = Series(pd.to_timedelta(['00:05:03'] * 3))
td2 = pd.to_timedelta('00:05:04')
result = td1 - td2
expected = Series([timedelta(seconds=0)] * 3) -Series(
[timedelta(seconds=1)] * 3)
self.assert_(result.dtype == 'm8[ns]')
assert_series_equal(result, expected)

result2 = td2 - td1
expected = (Series([timedelta(seconds=1)] * 3) -
Series([timedelta(seconds=0)] * 3))
assert_series_equal(result2, expected)

# roundtrip
assert_series_equal(result + td2,td1)

def test_timedelta64_operations_with_integers(self):

# GH 4521
Expand Down
13 changes: 8 additions & 5 deletions pandas/tseries/tests/test_timedeltas.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,17 +180,20 @@ def test_timedelta_ops(self):
s = Series([Timestamp('20130101') + timedelta(seconds=i*i) for i in range(10) ])
td = s.diff()

result = td.mean()
result = td.mean()[0]
# TODO This should have returned a scalar to begin with. Hack for now.
expected = to_timedelta(timedelta(seconds=9))
tm.assert_series_equal(result, expected)
tm.assert_almost_equal(result, expected)

result = td.quantile(.1)
# This properly returned a scalar.
expected = to_timedelta('00:00:02.6')
tm.assert_series_equal(result, expected)
tm.assert_almost_equal(result, expected)

result = td.median()
result = td.median()[0]
# TODO This should have returned a scalar to begin with. Hack for now.
expected = to_timedelta('00:00:08')
tm.assert_series_equal(result, expected)
tm.assert_almost_equal(result, expected)

if __name__ == '__main__':
nose.runmodule(argv=[__file__, '-vvs', '-x', '--pdb', '--pdb-failure'],
Expand Down
3 changes: 2 additions & 1 deletion pandas/tseries/timedeltas.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ def _convert_listlike(arg, box):
elif is_list_like(arg):
return _convert_listlike(arg, box=box)

return _convert_listlike([ arg ], box=box)
# ...so it must be a scalar value. Return scalar.
return _coerce_scalar_to_timedelta_type(arg, unit=unit)

_short_search = re.compile(
"^\s*(?P<neg>-?)\s*(?P<value>\d*\.?\d*)\s*(?P<unit>d|s|ms|us|ns)?\s*$",re.IGNORECASE)
Expand Down