Skip to content

Commit 306e647

Browse files
gfyoungjorisvandenbossche
authored andcommitted
DEPR: Deprecated PeriodIndex.to_datetime (#14113)
Deprecation is in favour of PeriodIndex.to_timestamp.
1 parent 6483180 commit 306e647

File tree

4 files changed

+29
-6
lines changed

4 files changed

+29
-6
lines changed

doc/source/whatsnew/v0.19.0.txt

+1
Original file line numberDiff line numberDiff line change
@@ -1130,6 +1130,7 @@ Deprecations
11301130
- ``Categorical.reshape`` has been deprecated and will be removed in a subsequent release (:issue:`12882`)
11311131
- ``Series.reshape`` has been deprecated and will be removed in a subsequent release (:issue:`12882`)
11321132

1133+
- ``PeriodIndex.to_datetime`` has been deprecated in favour of ``PeriodIndex.to_timestamp`` (:issue:`8254`)
11331134
- ``Timestamp.to_datetime`` has been deprecated in favour of ``Timestamp.to_pydatetime`` (:issue:`8254`)
11341135
- ``Index.to_datetime`` and ``DatetimeIndex.to_datetime`` have been deprecated in favour of ``pd.to_datetime`` (:issue:`8254`)
11351136
- ``SparseList`` has been deprecated and will be removed in a future version (:issue:`13784`)

pandas/tests/frame/test_to_csv.py

+10-4
Original file line numberDiff line numberDiff line change
@@ -291,10 +291,10 @@ def _to_uni(x):
291291
elif r_dtype == 'p':
292292
r_dtype = 'O'
293293
recons.index = np.array(
294-
list(map(Timestamp, recons.index.to_datetime())),
294+
list(map(Timestamp, to_datetime(recons.index))),
295295
dtype=r_dtype)
296296
df.index = np.array(
297-
list(map(Timestamp, df.index.to_datetime())),
297+
list(map(Timestamp, df.index.to_timestamp())),
298298
dtype=r_dtype)
299299
else:
300300
r_dtype = type_map.get(r_dtype)
@@ -316,10 +316,10 @@ def _to_uni(x):
316316
elif c_dtype == 'p':
317317
c_dtype = 'O'
318318
recons.columns = np.array(
319-
lmap(Timestamp, recons.columns.to_datetime()),
319+
lmap(Timestamp, to_datetime(recons.columns)),
320320
dtype=c_dtype)
321321
df.columns = np.array(
322-
lmap(Timestamp, df.columns.to_datetime()),
322+
lmap(Timestamp, df.columns.to_timestamp()),
323323
dtype=c_dtype)
324324
else:
325325
c_dtype = type_map.get(c_dtype)
@@ -1157,3 +1157,9 @@ def test_to_csv_quoting(self):
11571157
df = df.set_index(['a', 'b'])
11581158
expected = '"a","b","c"\n"1","3","5"\n"2","4","6"\n'
11591159
self.assertEqual(df.to_csv(quoting=csv.QUOTE_ALL), expected)
1160+
1161+
1162+
if __name__ == '__main__':
1163+
import nose
1164+
nose.runmodule(argv=[__file__, '-vvs', '-x', '--pdb', '--pdb-failure'],
1165+
exit=False)

pandas/tseries/period.py

+8
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# pylint: disable=E1101,E1103,W0232
22
from datetime import datetime, timedelta
33
import numpy as np
4+
import warnings
45

56

67
from pandas.core import common as com
@@ -550,6 +551,13 @@ def asfreq(self, freq=None, how='E'):
550551
return self._simple_new(new_data, self.name, freq=freq)
551552

552553
def to_datetime(self, dayfirst=False):
554+
"""
555+
DEPRECATED: use :meth:`to_timestamp` instead.
556+
557+
Cast to DatetimeIndex.
558+
"""
559+
warnings.warn("to_datetime is deprecated. Use self.to_timestamp(...)",
560+
FutureWarning, stacklevel=2)
553561
return self.to_timestamp()
554562

555563
year = _field_accessor('year', 0, "The year of the period")

pandas/tseries/tests/test_period.py

+10-2
Original file line numberDiff line numberDiff line change
@@ -3567,12 +3567,20 @@ def test_with_multi_index(self):
35673567

35683568
tm.assertIsInstance(s.index.values[0][0], Period)
35693569

3570-
def test_to_datetime_1703(self):
3570+
def test_to_timestamp_1703(self):
35713571
index = period_range('1/1/2012', periods=4, freq='D')
35723572

3573-
result = index.to_datetime()
3573+
result = index.to_timestamp()
35743574
self.assertEqual(result[0], Timestamp('1/1/2012'))
35753575

3576+
def test_to_datetime_depr(self):
3577+
index = period_range('1/1/2012', periods=4, freq='D')
3578+
3579+
with tm.assert_produces_warning(FutureWarning,
3580+
check_stacklevel=False):
3581+
result = index.to_datetime()
3582+
self.assertEqual(result[0], Timestamp('1/1/2012'))
3583+
35763584
def test_get_loc_msg(self):
35773585
idx = period_range('2000-1-1', freq='A', periods=10)
35783586
bad_period = Period('2012', 'A')

0 commit comments

Comments
 (0)