Skip to content

API: (GH3888) more consistency in the to_datetime return types (given string/array of string inputs) #3890

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 2 commits into from
Jun 19, 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
3 changes: 3 additions & 0 deletions RELEASE.rst
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ pandas 0.11.1
- ``read_html`` now defaults to ``None`` when reading, and falls back on
``bs4`` + ``html5lib`` when lxml fails to parse. a list of parsers to try
until success is also valid
- more consistency in the to_datetime return types (give string/array of string inputs) (GH3888_)

**Bug Fixes**

Expand Down Expand Up @@ -355,6 +356,8 @@ pandas 0.11.1
.. _GH3911: https://github.com/pydata/pandas/issues/3911
.. _GH3912: https://github.com/pydata/pandas/issues/3912
.. _GH3764: https://github.com/pydata/pandas/issues/3764
.. _GH3888: https://github.com/pydata/pandas/issues/3888


pandas 0.11.0
=============
Expand Down
10 changes: 7 additions & 3 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -1159,9 +1159,13 @@ def truncate(self, before=None, after=None, copy=True):
-------
truncated : type of caller
"""
from pandas.tseries.tools import to_datetime
before = to_datetime(before)
after = to_datetime(after)

# if we have a date index, convert to dates, otherwise
# treat like a slice
if self.index.is_all_dates:
from pandas.tseries.tools import to_datetime
before = to_datetime(before)
after = to_datetime(after)

if before is not None and after is not None:
if before > after:
Expand Down
27 changes: 25 additions & 2 deletions pandas/tseries/tests/test_timeseries.py
Original file line number Diff line number Diff line change
Expand Up @@ -825,13 +825,36 @@ def test_nat_scalar_field_access(self):

self.assertEquals(NaT.weekday(), -1)

def test_to_datetime_empty_string(self):
def test_to_datetime_types(self):

# empty string
result = to_datetime('')
self.assert_(result == '')
self.assert_(result is NaT)

result = to_datetime(['', ''])
self.assert_(isnull(result).all())

# ints
result = Timestamp(0)
expected = to_datetime(0)
self.assert_(result == expected)

# GH 3888 (strings)
expected = to_datetime(['2012'])[0]
result = to_datetime('2012')
self.assert_(result == expected)

### array = ['2012','20120101','20120101 12:01:01']
array = ['20120101','20120101 12:01:01']
expected = list(to_datetime(array))
result = map(Timestamp,array)
tm.assert_almost_equal(result,expected)

### currently fails ###
### result = Timestamp('2012')
### expected = to_datetime('2012')
### self.assert_(result == expected)

def test_to_datetime_other_datetime64_units(self):
# 5/25/2012
scalar = np.int64(1337904000000000).view('M8[us]')
Expand Down
38 changes: 14 additions & 24 deletions pandas/tseries/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,16 +99,7 @@ def _convert_f(arg):
except (ValueError, TypeError):
raise e

if arg is None:
return arg
elif isinstance(arg, datetime):
return arg
elif isinstance(arg, Series):
values = arg.values
if not com.is_datetime64_dtype(values):
values = _convert_f(values)
return Series(values, index=arg.index, name=arg.name)
elif isinstance(arg, (np.ndarray, list)):
def _convert_listlike(arg):
if isinstance(arg, list):
arg = np.array(arg, dtype='O')

Expand All @@ -122,24 +113,23 @@ def _convert_f(arg):
return DatetimeIndex._simple_new(values, None, tz=tz)
except (ValueError, TypeError):
raise e
return arg
return arg

try:
return _convert_f(arg)
except ValueError:
raise
return arg
return _convert_f(arg)

try:
if not arg:
return arg
default = datetime(1, 1, 1)
return parse(arg, dayfirst=dayfirst, default=default)
except Exception:
if errors == 'raise':
raise
if arg is None:
return arg
elif isinstance(arg, datetime):
return arg
elif isinstance(arg, Series):
values = arg.values
if not com.is_datetime64_dtype(values):
values = _convert_f(values)
return Series(values, index=arg.index, name=arg.name)
elif isinstance(arg, (np.ndarray, list)):
return _convert_listlike(arg)

return _convert_listlike(np.array([ arg ], dtype='O'))[0]

class DateParseError(ValueError):
pass
Expand Down