Skip to content

Commit b03df73

Browse files
committed
Merge pull request #3890 from jreback/to_datetime
API: (GH3888) more consistency in the to_datetime return types (given string/array of string inputs)
2 parents 028c9ed + e97b3a6 commit b03df73

File tree

4 files changed

+49
-29
lines changed

4 files changed

+49
-29
lines changed

RELEASE.rst

+3
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ pandas 0.11.1
159159
- ``read_html`` now defaults to ``None`` when reading, and falls back on
160160
``bs4`` + ``html5lib`` when lxml fails to parse. a list of parsers to try
161161
until success is also valid
162+
- more consistency in the to_datetime return types (give string/array of string inputs) (GH3888_)
162163

163164
**Bug Fixes**
164165

@@ -355,6 +356,8 @@ pandas 0.11.1
355356
.. _GH3911: https://github.com/pydata/pandas/issues/3911
356357
.. _GH3912: https://github.com/pydata/pandas/issues/3912
357358
.. _GH3764: https://github.com/pydata/pandas/issues/3764
359+
.. _GH3888: https://github.com/pydata/pandas/issues/3888
360+
358361

359362
pandas 0.11.0
360363
=============

pandas/core/generic.py

+7-3
Original file line numberDiff line numberDiff line change
@@ -1159,9 +1159,13 @@ def truncate(self, before=None, after=None, copy=True):
11591159
-------
11601160
truncated : type of caller
11611161
"""
1162-
from pandas.tseries.tools import to_datetime
1163-
before = to_datetime(before)
1164-
after = to_datetime(after)
1162+
1163+
# if we have a date index, convert to dates, otherwise
1164+
# treat like a slice
1165+
if self.index.is_all_dates:
1166+
from pandas.tseries.tools import to_datetime
1167+
before = to_datetime(before)
1168+
after = to_datetime(after)
11651169

11661170
if before is not None and after is not None:
11671171
if before > after:

pandas/tseries/tests/test_timeseries.py

+25-2
Original file line numberDiff line numberDiff line change
@@ -825,13 +825,36 @@ def test_nat_scalar_field_access(self):
825825

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

828-
def test_to_datetime_empty_string(self):
828+
def test_to_datetime_types(self):
829+
830+
# empty string
829831
result = to_datetime('')
830-
self.assert_(result == '')
832+
self.assert_(result is NaT)
831833

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

837+
# ints
838+
result = Timestamp(0)
839+
expected = to_datetime(0)
840+
self.assert_(result == expected)
841+
842+
# GH 3888 (strings)
843+
expected = to_datetime(['2012'])[0]
844+
result = to_datetime('2012')
845+
self.assert_(result == expected)
846+
847+
### array = ['2012','20120101','20120101 12:01:01']
848+
array = ['20120101','20120101 12:01:01']
849+
expected = list(to_datetime(array))
850+
result = map(Timestamp,array)
851+
tm.assert_almost_equal(result,expected)
852+
853+
### currently fails ###
854+
### result = Timestamp('2012')
855+
### expected = to_datetime('2012')
856+
### self.assert_(result == expected)
857+
835858
def test_to_datetime_other_datetime64_units(self):
836859
# 5/25/2012
837860
scalar = np.int64(1337904000000000).view('M8[us]')

pandas/tseries/tools.py

+14-24
Original file line numberDiff line numberDiff line change
@@ -99,16 +99,7 @@ def _convert_f(arg):
9999
except (ValueError, TypeError):
100100
raise e
101101

102-
if arg is None:
103-
return arg
104-
elif isinstance(arg, datetime):
105-
return arg
106-
elif isinstance(arg, Series):
107-
values = arg.values
108-
if not com.is_datetime64_dtype(values):
109-
values = _convert_f(values)
110-
return Series(values, index=arg.index, name=arg.name)
111-
elif isinstance(arg, (np.ndarray, list)):
102+
def _convert_listlike(arg):
112103
if isinstance(arg, list):
113104
arg = np.array(arg, dtype='O')
114105

@@ -122,24 +113,23 @@ def _convert_f(arg):
122113
return DatetimeIndex._simple_new(values, None, tz=tz)
123114
except (ValueError, TypeError):
124115
raise e
125-
return arg
116+
return arg
126117

127-
try:
128-
return _convert_f(arg)
129-
except ValueError:
130-
raise
131-
return arg
118+
return _convert_f(arg)
132119

133-
try:
134-
if not arg:
135-
return arg
136-
default = datetime(1, 1, 1)
137-
return parse(arg, dayfirst=dayfirst, default=default)
138-
except Exception:
139-
if errors == 'raise':
140-
raise
120+
if arg is None:
141121
return arg
122+
elif isinstance(arg, datetime):
123+
return arg
124+
elif isinstance(arg, Series):
125+
values = arg.values
126+
if not com.is_datetime64_dtype(values):
127+
values = _convert_f(values)
128+
return Series(values, index=arg.index, name=arg.name)
129+
elif isinstance(arg, (np.ndarray, list)):
130+
return _convert_listlike(arg)
142131

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

144134
class DateParseError(ValueError):
145135
pass

0 commit comments

Comments
 (0)