Skip to content

Commit 59082e9

Browse files
committed
ENH: allow construction of datetimes from columns in a DataFrame
closes #8158 Author: Jeff Reback <[email protected]> Closes #12967 from jreback/dates and squashes the following commits: e18c9cc [Jeff Reback] TST: move .to_datetime() tests to new testing class 7dc9406 [Jeff Reback] ENH: allow construction of datetimes from columns in a DataFrame
1 parent 6994240 commit 59082e9

File tree

5 files changed

+452
-164
lines changed

5 files changed

+452
-164
lines changed

doc/source/timeseries.rst

+21-2
Original file line numberDiff line numberDiff line change
@@ -189,9 +189,28 @@ or ``format``, use ``to_datetime`` if these are required.
189189

190190
.. ipython:: python
191191
192-
to_datetime('2010/11/12')
192+
pd.to_datetime('2010/11/12')
193193
194-
Timestamp('2010/11/12')
194+
pd.Timestamp('2010/11/12')
195+
196+
.. versionadded:: 0.18.1
197+
198+
You can also pass a ``DataFrame`` of integer or string columns to assemble into a ``Series`` of ``Timestamps``.
199+
200+
.. ipython:: python
201+
202+
df = pd.pd.DataFrame({'year': [2015, 2016],
203+
'month': [2, 3],
204+
'day': [4, 5],
205+
'hour': [2, 3]})
206+
pd.to_datetime(df)
207+
208+
209+
You can pass only the columns that you need to assemble.
210+
211+
.. ipython:: python
212+
213+
pd.to_datetime(df[['year', 'month', 'day']])
195214
196215
197216
Invalid Data

doc/source/whatsnew/v0.18.1.txt

+18
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,24 @@ Partial string indexing now matches on ``DateTimeIndex`` when part of a ``MultiI
120120
dft2 = dft2.swaplevel(0, 1).sort_index()
121121
dft2.loc[idx[:, '2013-01-05'], :]
122122

123+
.. _whatsnew_0181.enhancements.assembling:
124+
125+
Assembling Datetimes
126+
^^^^^^^^^^^^^^^^^^^^
127+
128+
``pd.to_datetime()`` has gained the ability to assemble datetimes from a passed in ``DataFrame`` or a dict. (:issue:`8158`).
129+
130+
.. ipython:: python
131+
132+
df = pd.DataFrame({'year': [2015, 2016],
133+
'month': [2, 3],
134+
'day': [4, 5],
135+
'hour': [2, 3]})
136+
pd.to_datetime(df)
137+
138+
# pass only the columns that you need to assemble
139+
pd.to_datetime(df[['year', 'month', 'day']])
140+
123141
.. _whatsnew_0181.other:
124142

125143
Other Enhancements

pandas/tseries/tests/test_period.py

-10
Original file line numberDiff line numberDiff line change
@@ -3144,16 +3144,6 @@ def test_to_datetime_1703(self):
31443144
result = index.to_datetime()
31453145
self.assertEqual(result[0], Timestamp('1/1/2012'))
31463146

3147-
def test_to_datetime_dimensions(self):
3148-
# GH 11776
3149-
df = DataFrame({'a': ['1/1/2012', '1/2/2012'],
3150-
'b': ['12/30/2012', '12/31/2012']})
3151-
with tm.assertRaisesRegexp(TypeError, "1-d array"):
3152-
to_datetime(df)
3153-
for errors in ['ignore', 'raise', 'coerce']:
3154-
with tm.assertRaisesRegexp(TypeError, "1-d array"):
3155-
to_datetime(df, errors=errors)
3156-
31573147
def test_get_loc_msg(self):
31583148
idx = period_range('2000-1-1', freq='A', periods=10)
31593149
bad_period = Period('2012', 'A')

0 commit comments

Comments
 (0)