Skip to content

Commit 670435a

Browse files
gfyoungjorisvandenbossche
authored andcommitted
DEPR: Deprecated Index.to_datetime (#14096)
Partially addresses gh-8254. Closes gh-8612 because pd.to_datetime has a format arg.
1 parent 0e61847 commit 670435a

File tree

4 files changed

+29
-35
lines changed

4 files changed

+29
-35
lines changed

doc/source/whatsnew/v0.19.0.txt

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

1034+
- ``Index.to_datetime`` and ``DatetimeIndex.to_datetime`` have been deprecated in favour of ``pd.to_datetime`` (:issue:`8254`)
10341035
- ``SparseList`` has been deprecated and will be removed in a future version (:issue:`13784`)
10351036
- ``DataFrame.to_html()`` and ``DataFrame.to_latex()`` have dropped the ``colSpace`` parameter in favor of ``col_space`` (:issue:`13857`)
10361037
- ``DataFrame.to_sql()`` has deprecated the ``flavor`` parameter, as it is superfluous when SQLAlchemy is not installed (:issue:`13611`)

pandas/indexes/base.py

+5
Original file line numberDiff line numberDiff line change
@@ -851,9 +851,14 @@ def _to_safe_for_reshape(self):
851851

852852
def to_datetime(self, dayfirst=False):
853853
"""
854+
DEPRECATED: use :meth:`pandas.to_datetime` instead.
855+
854856
For an Index containing strings or datetime.datetime objects, attempt
855857
conversion to DatetimeIndex
856858
"""
859+
warnings.warn("to_datetime is deprecated. Use pd.to_datetime(...)",
860+
FutureWarning, stacklevel=2)
861+
857862
from pandas.tseries.index import DatetimeIndex
858863
if self.inferred_type == 'string':
859864
from dateutil.parser import parse

pandas/io/tests/parser/parse_dates.py

+11-27
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@
2020

2121
from pandas import DataFrame, Series, Index, DatetimeIndex
2222
from pandas import compat
23-
from pandas.compat import(parse_date, StringIO,
24-
lrange, lmap)
23+
from pandas.compat import parse_date, StringIO, lrange
2524
from pandas.tseries.index import date_range
2625

2726

@@ -291,33 +290,18 @@ def test_yy_format_with_yearfirst(self):
291290
tm.assert_frame_equal(rs, xp)
292291

293292
def test_parse_dates_column_list(self):
294-
from pandas.core.datetools import to_datetime
295-
296-
data = '''date;destination;ventilationcode;unitcode;units;aux_date
297-
01/01/2010;P;P;50;1;12/1/2011
298-
01/01/2010;P;R;50;1;13/1/2011
299-
15/01/2010;P;P;50;1;14/1/2011
300-
01/05/2010;P;P;50;1;15/1/2011'''
301-
302-
expected = self.read_csv(StringIO(data), sep=";", index_col=lrange(4))
303-
304-
lev = expected.index.levels[0]
305-
levels = list(expected.index.levels)
306-
levels[0] = lev.to_datetime(dayfirst=True)
307-
# hack to get this to work - remove for final test
308-
levels[0].name = lev.name
309-
expected.index.set_levels(levels, inplace=True)
310-
expected['aux_date'] = to_datetime(expected['aux_date'],
311-
dayfirst=True)
312-
expected['aux_date'] = lmap(Timestamp, expected['aux_date'])
313-
tm.assertIsInstance(expected['aux_date'][0], datetime)
314-
315-
df = self.read_csv(StringIO(data), sep=";", index_col=lrange(4),
316-
parse_dates=[0, 5], dayfirst=True)
293+
data = 'a,b,c\n01/01/2010,1,15/02/2010'
294+
295+
expected = DataFrame({'a': [datetime(2010, 1, 1)], 'b': [1],
296+
'c': [datetime(2010, 2, 15)]})
297+
expected = expected.set_index(['a', 'b'])
298+
299+
df = self.read_csv(StringIO(data), index_col=[0, 1],
300+
parse_dates=[0, 2], dayfirst=True)
317301
tm.assert_frame_equal(df, expected)
318302

319-
df = self.read_csv(StringIO(data), sep=";", index_col=lrange(4),
320-
parse_dates=['date', 'aux_date'], dayfirst=True)
303+
df = self.read_csv(StringIO(data), index_col=[0, 1],
304+
parse_dates=['a', 'c'], dayfirst=True)
321305
tm.assert_frame_equal(df, expected)
322306

323307
def test_multi_index_parse_dates(self):

pandas/tseries/tests/test_timeseries.py

+12-8
Original file line numberDiff line numberDiff line change
@@ -2535,15 +2535,19 @@ def test_unit_mixed(self):
25352535
def test_index_to_datetime(self):
25362536
idx = Index(['1/1/2000', '1/2/2000', '1/3/2000'])
25372537

2538-
result = idx.to_datetime()
2539-
expected = DatetimeIndex(datetools.to_datetime(idx.values))
2540-
tm.assert_index_equal(result, expected)
2538+
with tm.assert_produces_warning(FutureWarning,
2539+
check_stacklevel=False):
2540+
result = idx.to_datetime()
2541+
expected = DatetimeIndex(datetools.to_datetime(idx.values))
2542+
tm.assert_index_equal(result, expected)
25412543

2542-
today = datetime.today()
2543-
idx = Index([today], dtype=object)
2544-
result = idx.to_datetime()
2545-
expected = DatetimeIndex([today])
2546-
tm.assert_index_equal(result, expected)
2544+
with tm.assert_produces_warning(FutureWarning,
2545+
check_stacklevel=False):
2546+
today = datetime.today()
2547+
idx = Index([today], dtype=object)
2548+
result = idx.to_datetime()
2549+
expected = DatetimeIndex([today])
2550+
tm.assert_index_equal(result, expected)
25472551

25482552
def test_dataframe(self):
25492553

0 commit comments

Comments
 (0)