Skip to content

Commit e18c9cc

Browse files
committed
TST: move .to_datetime() tests to new testing class
1 parent 7dc9406 commit e18c9cc

File tree

3 files changed

+151
-155
lines changed

3 files changed

+151
-155
lines changed

doc/source/timeseries.rst

+1-4
Original file line numberDiff line numberDiff line change
@@ -195,8 +195,7 @@ or ``format``, use ``to_datetime`` if these are required.
195195
196196
.. versionadded:: 0.18.1
197197

198-
You can also pass a ``DataFrame`` of integer or string columns to assemble into a ``Series``
199-
of ``Timestamps``.
198+
You can also pass a ``DataFrame`` of integer or string columns to assemble into a ``Series`` of ``Timestamps``.
200199

201200
.. ipython:: python
202201
@@ -213,8 +212,6 @@ You can pass only the columns that you need to assemble.
213212
214213
pd.to_datetime(df[['year', 'month', 'day']])
215214
216-
.. _whatsnew_0181.other:
217-
218215
219216
Invalid Data
220217
~~~~~~~~~~~~

pandas/tseries/tests/test_timeseries.py

+149-150
Original file line numberDiff line numberDiff line change
@@ -1049,154 +1049,6 @@ def test_to_datetime_list_of_integers(self):
10491049

10501050
self.assertTrue(rng.equals(result))
10511051

1052-
def test_to_datetime_dt64s(self):
1053-
in_bound_dts = [
1054-
np.datetime64('2000-01-01'),
1055-
np.datetime64('2000-01-02'),
1056-
]
1057-
1058-
for dt in in_bound_dts:
1059-
self.assertEqual(pd.to_datetime(dt), Timestamp(dt))
1060-
1061-
oob_dts = [np.datetime64('1000-01-01'), np.datetime64('5000-01-02'), ]
1062-
1063-
for dt in oob_dts:
1064-
self.assertRaises(ValueError, pd.to_datetime, dt, errors='raise')
1065-
self.assertRaises(ValueError, tslib.Timestamp, dt)
1066-
self.assertIs(pd.to_datetime(dt, errors='coerce'), NaT)
1067-
1068-
def test_to_datetime_array_of_dt64s(self):
1069-
dts = [np.datetime64('2000-01-01'), np.datetime64('2000-01-02'), ]
1070-
1071-
# Assuming all datetimes are in bounds, to_datetime() returns
1072-
# an array that is equal to Timestamp() parsing
1073-
self.assert_numpy_array_equal(
1074-
pd.to_datetime(dts, box=False),
1075-
np.array([Timestamp(x).asm8 for x in dts])
1076-
)
1077-
1078-
# A list of datetimes where the last one is out of bounds
1079-
dts_with_oob = dts + [np.datetime64('9999-01-01')]
1080-
1081-
self.assertRaises(ValueError, pd.to_datetime, dts_with_oob,
1082-
errors='raise')
1083-
1084-
self.assert_numpy_array_equal(
1085-
pd.to_datetime(dts_with_oob, box=False, errors='coerce'),
1086-
np.array(
1087-
[
1088-
Timestamp(dts_with_oob[0]).asm8,
1089-
Timestamp(dts_with_oob[1]).asm8,
1090-
iNaT,
1091-
],
1092-
dtype='M8'
1093-
)
1094-
)
1095-
1096-
# With errors='ignore', out of bounds datetime64s
1097-
# are converted to their .item(), which depending on the version of
1098-
# numpy is either a python datetime.datetime or datetime.date
1099-
self.assert_numpy_array_equal(
1100-
pd.to_datetime(dts_with_oob, box=False, errors='ignore'),
1101-
np.array(
1102-
[dt.item() for dt in dts_with_oob],
1103-
dtype='O'
1104-
)
1105-
)
1106-
1107-
def test_to_datetime_tz(self):
1108-
1109-
# xref 8260
1110-
# uniform returns a DatetimeIndex
1111-
arr = [pd.Timestamp('2013-01-01 13:00:00-0800', tz='US/Pacific'),
1112-
pd.Timestamp('2013-01-02 14:00:00-0800', tz='US/Pacific')]
1113-
result = pd.to_datetime(arr)
1114-
expected = DatetimeIndex(
1115-
['2013-01-01 13:00:00', '2013-01-02 14:00:00'], tz='US/Pacific')
1116-
tm.assert_index_equal(result, expected)
1117-
1118-
# mixed tzs will raise
1119-
arr = [pd.Timestamp('2013-01-01 13:00:00', tz='US/Pacific'),
1120-
pd.Timestamp('2013-01-02 14:00:00', tz='US/Eastern')]
1121-
self.assertRaises(ValueError, lambda: pd.to_datetime(arr))
1122-
1123-
def test_to_datetime_tz_pytz(self):
1124-
1125-
# xref 8260
1126-
tm._skip_if_no_pytz()
1127-
import pytz
1128-
1129-
us_eastern = pytz.timezone('US/Eastern')
1130-
arr = np.array([us_eastern.localize(datetime(year=2000, month=1, day=1,
1131-
hour=3, minute=0)),
1132-
us_eastern.localize(datetime(year=2000, month=6, day=1,
1133-
hour=3, minute=0))],
1134-
dtype=object)
1135-
result = pd.to_datetime(arr, utc=True)
1136-
expected = DatetimeIndex(['2000-01-01 08:00:00+00:00',
1137-
'2000-06-01 07:00:00+00:00'],
1138-
dtype='datetime64[ns, UTC]', freq=None)
1139-
tm.assert_index_equal(result, expected)
1140-
1141-
def test_to_datetime_utc_is_true(self):
1142-
# See gh-11934
1143-
start = pd.Timestamp('2014-01-01', tz='utc')
1144-
end = pd.Timestamp('2014-01-03', tz='utc')
1145-
date_range = pd.bdate_range(start, end)
1146-
1147-
result = pd.to_datetime(date_range, utc=True)
1148-
expected = pd.DatetimeIndex(data=date_range)
1149-
tm.assert_index_equal(result, expected)
1150-
1151-
def test_to_datetime_tz_psycopg2(self):
1152-
1153-
# xref 8260
1154-
try:
1155-
import psycopg2
1156-
except ImportError:
1157-
raise nose.SkipTest("no psycopg2 installed")
1158-
1159-
# misc cases
1160-
tz1 = psycopg2.tz.FixedOffsetTimezone(offset=-300, name=None)
1161-
tz2 = psycopg2.tz.FixedOffsetTimezone(offset=-240, name=None)
1162-
arr = np.array([datetime(2000, 1, 1, 3, 0, tzinfo=tz1),
1163-
datetime(2000, 6, 1, 3, 0, tzinfo=tz2)],
1164-
dtype=object)
1165-
1166-
result = pd.to_datetime(arr, errors='coerce', utc=True)
1167-
expected = DatetimeIndex(['2000-01-01 08:00:00+00:00',
1168-
'2000-06-01 07:00:00+00:00'],
1169-
dtype='datetime64[ns, UTC]', freq=None)
1170-
tm.assert_index_equal(result, expected)
1171-
1172-
# dtype coercion
1173-
i = pd.DatetimeIndex([
1174-
'2000-01-01 08:00:00+00:00'
1175-
], tz=psycopg2.tz.FixedOffsetTimezone(offset=-300, name=None))
1176-
self.assertFalse(com.is_datetime64_ns_dtype(i))
1177-
1178-
# tz coerceion
1179-
result = pd.to_datetime(i, errors='coerce')
1180-
tm.assert_index_equal(result, i)
1181-
1182-
result = pd.to_datetime(i, errors='coerce', utc=True)
1183-
expected = pd.DatetimeIndex(['2000-01-01 13:00:00'],
1184-
dtype='datetime64[ns, UTC]')
1185-
tm.assert_index_equal(result, expected)
1186-
1187-
def test_index_to_datetime(self):
1188-
idx = Index(['1/1/2000', '1/2/2000', '1/3/2000'])
1189-
1190-
result = idx.to_datetime()
1191-
expected = DatetimeIndex(datetools.to_datetime(idx.values))
1192-
self.assertTrue(result.equals(expected))
1193-
1194-
today = datetime.today()
1195-
idx = Index([today], dtype=object)
1196-
result = idx.to_datetime()
1197-
expected = DatetimeIndex([today])
1198-
self.assertTrue(result.equals(expected))
1199-
12001052
def test_to_datetime_freq(self):
12011053
xp = bdate_range('2000-1-1', periods=10, tz='UTC')
12021054
rs = xp.to_datetime()
@@ -2286,8 +2138,153 @@ def _simple_ts(start, end, freq='D'):
22862138
class TestToDatetime(tm.TestCase):
22872139
_multiprocess_can_split_ = True
22882140

2289-
# TODO: move all to_datetime tests not covered in other
2290-
# classes here
2141+
def test_to_datetime_dt64s(self):
2142+
in_bound_dts = [
2143+
np.datetime64('2000-01-01'),
2144+
np.datetime64('2000-01-02'),
2145+
]
2146+
2147+
for dt in in_bound_dts:
2148+
self.assertEqual(pd.to_datetime(dt), Timestamp(dt))
2149+
2150+
oob_dts = [np.datetime64('1000-01-01'), np.datetime64('5000-01-02'), ]
2151+
2152+
for dt in oob_dts:
2153+
self.assertRaises(ValueError, pd.to_datetime, dt, errors='raise')
2154+
self.assertRaises(ValueError, tslib.Timestamp, dt)
2155+
self.assertIs(pd.to_datetime(dt, errors='coerce'), NaT)
2156+
2157+
def test_to_datetime_array_of_dt64s(self):
2158+
dts = [np.datetime64('2000-01-01'), np.datetime64('2000-01-02'), ]
2159+
2160+
# Assuming all datetimes are in bounds, to_datetime() returns
2161+
# an array that is equal to Timestamp() parsing
2162+
self.assert_numpy_array_equal(
2163+
pd.to_datetime(dts, box=False),
2164+
np.array([Timestamp(x).asm8 for x in dts])
2165+
)
2166+
2167+
# A list of datetimes where the last one is out of bounds
2168+
dts_with_oob = dts + [np.datetime64('9999-01-01')]
2169+
2170+
self.assertRaises(ValueError, pd.to_datetime, dts_with_oob,
2171+
errors='raise')
2172+
2173+
self.assert_numpy_array_equal(
2174+
pd.to_datetime(dts_with_oob, box=False, errors='coerce'),
2175+
np.array(
2176+
[
2177+
Timestamp(dts_with_oob[0]).asm8,
2178+
Timestamp(dts_with_oob[1]).asm8,
2179+
iNaT,
2180+
],
2181+
dtype='M8'
2182+
)
2183+
)
2184+
2185+
# With errors='ignore', out of bounds datetime64s
2186+
# are converted to their .item(), which depending on the version of
2187+
# numpy is either a python datetime.datetime or datetime.date
2188+
self.assert_numpy_array_equal(
2189+
pd.to_datetime(dts_with_oob, box=False, errors='ignore'),
2190+
np.array(
2191+
[dt.item() for dt in dts_with_oob],
2192+
dtype='O'
2193+
)
2194+
)
2195+
2196+
def test_to_datetime_tz(self):
2197+
2198+
# xref 8260
2199+
# uniform returns a DatetimeIndex
2200+
arr = [pd.Timestamp('2013-01-01 13:00:00-0800', tz='US/Pacific'),
2201+
pd.Timestamp('2013-01-02 14:00:00-0800', tz='US/Pacific')]
2202+
result = pd.to_datetime(arr)
2203+
expected = DatetimeIndex(
2204+
['2013-01-01 13:00:00', '2013-01-02 14:00:00'], tz='US/Pacific')
2205+
tm.assert_index_equal(result, expected)
2206+
2207+
# mixed tzs will raise
2208+
arr = [pd.Timestamp('2013-01-01 13:00:00', tz='US/Pacific'),
2209+
pd.Timestamp('2013-01-02 14:00:00', tz='US/Eastern')]
2210+
self.assertRaises(ValueError, lambda: pd.to_datetime(arr))
2211+
2212+
def test_to_datetime_tz_pytz(self):
2213+
2214+
# xref 8260
2215+
tm._skip_if_no_pytz()
2216+
import pytz
2217+
2218+
us_eastern = pytz.timezone('US/Eastern')
2219+
arr = np.array([us_eastern.localize(datetime(year=2000, month=1, day=1,
2220+
hour=3, minute=0)),
2221+
us_eastern.localize(datetime(year=2000, month=6, day=1,
2222+
hour=3, minute=0))],
2223+
dtype=object)
2224+
result = pd.to_datetime(arr, utc=True)
2225+
expected = DatetimeIndex(['2000-01-01 08:00:00+00:00',
2226+
'2000-06-01 07:00:00+00:00'],
2227+
dtype='datetime64[ns, UTC]', freq=None)
2228+
tm.assert_index_equal(result, expected)
2229+
2230+
def test_to_datetime_utc_is_true(self):
2231+
# See gh-11934
2232+
start = pd.Timestamp('2014-01-01', tz='utc')
2233+
end = pd.Timestamp('2014-01-03', tz='utc')
2234+
date_range = pd.bdate_range(start, end)
2235+
2236+
result = pd.to_datetime(date_range, utc=True)
2237+
expected = pd.DatetimeIndex(data=date_range)
2238+
tm.assert_index_equal(result, expected)
2239+
2240+
def test_to_datetime_tz_psycopg2(self):
2241+
2242+
# xref 8260
2243+
try:
2244+
import psycopg2
2245+
except ImportError:
2246+
raise nose.SkipTest("no psycopg2 installed")
2247+
2248+
# misc cases
2249+
tz1 = psycopg2.tz.FixedOffsetTimezone(offset=-300, name=None)
2250+
tz2 = psycopg2.tz.FixedOffsetTimezone(offset=-240, name=None)
2251+
arr = np.array([datetime(2000, 1, 1, 3, 0, tzinfo=tz1),
2252+
datetime(2000, 6, 1, 3, 0, tzinfo=tz2)],
2253+
dtype=object)
2254+
2255+
result = pd.to_datetime(arr, errors='coerce', utc=True)
2256+
expected = DatetimeIndex(['2000-01-01 08:00:00+00:00',
2257+
'2000-06-01 07:00:00+00:00'],
2258+
dtype='datetime64[ns, UTC]', freq=None)
2259+
tm.assert_index_equal(result, expected)
2260+
2261+
# dtype coercion
2262+
i = pd.DatetimeIndex([
2263+
'2000-01-01 08:00:00+00:00'
2264+
], tz=psycopg2.tz.FixedOffsetTimezone(offset=-300, name=None))
2265+
self.assertFalse(com.is_datetime64_ns_dtype(i))
2266+
2267+
# tz coerceion
2268+
result = pd.to_datetime(i, errors='coerce')
2269+
tm.assert_index_equal(result, i)
2270+
2271+
result = pd.to_datetime(i, errors='coerce', utc=True)
2272+
expected = pd.DatetimeIndex(['2000-01-01 13:00:00'],
2273+
dtype='datetime64[ns, UTC]')
2274+
tm.assert_index_equal(result, expected)
2275+
2276+
def test_index_to_datetime(self):
2277+
idx = Index(['1/1/2000', '1/2/2000', '1/3/2000'])
2278+
2279+
result = idx.to_datetime()
2280+
expected = DatetimeIndex(datetools.to_datetime(idx.values))
2281+
self.assertTrue(result.equals(expected))
2282+
2283+
today = datetime.today()
2284+
idx = Index([today], dtype=object)
2285+
result = idx.to_datetime()
2286+
expected = DatetimeIndex([today])
2287+
self.assertTrue(result.equals(expected))
22912288

22922289
def test_dataframe(self):
22932290

@@ -2408,6 +2405,7 @@ def test_dataframe(self):
24082405
with self.assertRaises(ValueError):
24092406
to_datetime(df2)
24102407

2408+
24112409
class TestDatetimeIndex(tm.TestCase):
24122410
_multiprocess_can_split_ = True
24132411

@@ -4944,6 +4942,7 @@ def test_to_datetime_format_weeks(self):
49444942

49454943

49464944
class TestToDatetimeInferFormat(tm.TestCase):
4945+
49474946
def test_to_datetime_infer_datetime_format_consistent_format(self):
49484947
time_series = pd.Series(pd.date_range('20000101', periods=50,
49494948
freq='H'))

pandas/tseries/tools.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ def to_datetime(arg, errors='raise', dayfirst=False, yearfirst=False,
290290
291291
292292
Assembling a datetime from multiple columns of a DataFrame. The keys can be
293-
strplike (%Y, %m) or common abbreviations like ('year', 'month')
293+
strptime-like (%Y, %m) or common abbreviations like ('year', 'month')
294294
295295
>>> df = pd.DataFrame({'year': [2015, 2016],
296296
'month': [2, 3],

0 commit comments

Comments
 (0)