Skip to content

Commit 8d8cc27

Browse files
jbrockmendelKevin D Smith
authored and
Kevin D Smith
committed
DEPR: string indexing along index for datetimes (pandas-dev#36179)
1 parent 991a2d4 commit 8d8cc27

File tree

6 files changed

+36
-8
lines changed

6 files changed

+36
-8
lines changed

doc/source/user_guide/timeseries.rst

+12
Original file line numberDiff line numberDiff line change
@@ -579,7 +579,12 @@ This type of slicing will work on a ``DataFrame`` with a ``DatetimeIndex`` as we
579579
partial string selection is a form of label slicing, the endpoints **will be** included. This
580580
would include matching times on an included date:
581581

582+
.. warning::
583+
584+
Indexing ``DataFrame`` rows with strings is deprecated in pandas 1.2.0 and will be removed in a future version. Use ``frame.loc[dtstring]`` instead.
585+
582586
.. ipython:: python
587+
:okwarning:
583588
584589
dft = pd.DataFrame(np.random.randn(100000, 1), columns=['A'],
585590
index=pd.date_range('20130101', periods=100000, freq='T'))
@@ -590,24 +595,28 @@ This starts on the very first time in the month, and includes the last date and
590595
time for the month:
591596

592597
.. ipython:: python
598+
:okwarning:
593599
594600
dft['2013-1':'2013-2']
595601
596602
This specifies a stop time **that includes all of the times on the last day**:
597603

598604
.. ipython:: python
605+
:okwarning:
599606
600607
dft['2013-1':'2013-2-28']
601608
602609
This specifies an **exact** stop time (and is not the same as the above):
603610

604611
.. ipython:: python
612+
:okwarning:
605613
606614
dft['2013-1':'2013-2-28 00:00:00']
607615
608616
We are stopping on the included end-point as it is part of the index:
609617

610618
.. ipython:: python
619+
:okwarning:
611620
612621
dft['2013-1-15':'2013-1-15 12:30:00']
613622
@@ -631,6 +640,7 @@ We are stopping on the included end-point as it is part of the index:
631640
Slicing with string indexing also honors UTC offset.
632641

633642
.. ipython:: python
643+
:okwarning:
634644
635645
df = pd.DataFrame([0], index=pd.DatetimeIndex(['2019-01-01'], tz='US/Pacific'))
636646
df
@@ -681,6 +691,7 @@ If index resolution is second, then the minute-accurate timestamp gives a
681691
If the timestamp string is treated as a slice, it can be used to index ``DataFrame`` with ``[]`` as well.
682692

683693
.. ipython:: python
694+
:okwarning:
684695
685696
dft_minute = pd.DataFrame({'a': [1, 2, 3], 'b': [4, 5, 6]},
686697
index=series_minute.index)
@@ -2027,6 +2038,7 @@ You can pass in dates and strings to ``Series`` and ``DataFrame`` with ``PeriodI
20272038
Passing a string representing a lower frequency than ``PeriodIndex`` returns partial sliced data.
20282039

20292040
.. ipython:: python
2041+
:okwarning:
20302042
20312043
ps['2011']
20322044

doc/source/whatsnew/v0.11.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,7 @@ Enhancements
367367
- You can now select with a string from a DataFrame with a datelike index, in a similar way to a Series (:issue:`3070`)
368368

369369
.. ipython:: python
370+
:okwarning:
370371
371372
idx = pd.date_range("2001-10-1", periods=5, freq='M')
372373
ts = pd.Series(np.random.rand(len(idx)), index=idx)

doc/source/whatsnew/v1.2.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,7 @@ Deprecations
213213
- Date parser functions :func:`~pandas.io.date_converters.parse_date_time`, :func:`~pandas.io.date_converters.parse_date_fields`, :func:`~pandas.io.date_converters.parse_all_fields` and :func:`~pandas.io.date_converters.generic_parser` from ``pandas.io.date_converters`` are deprecated and will be removed in a future version; use :func:`to_datetime` instead (:issue:`35741`)
214214
- :meth:`DataFrame.lookup` is deprecated and will be removed in a future version, use :meth:`DataFrame.melt` and :meth:`DataFrame.loc` instead (:issue:`18682`)
215215
- The :meth:`Index.to_native_types` is deprecated. Use ``.astype(str)`` instead (:issue:`28867`)
216+
- Deprecated indexing :class:`DataFrame` rows with datetime-like strings ``df[string]``, use ``df.loc[string]`` instead (:issue:`36179`)
216217

217218
.. ---------------------------------------------------------------------------
218219

pandas/core/indexing.py

+10-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from typing import TYPE_CHECKING, Hashable, List, Tuple, Union
2+
import warnings
23

34
import numpy as np
45

@@ -2191,7 +2192,15 @@ def convert_to_index_sliceable(obj: "DataFrame", key):
21912192
# slice here via partial string indexing
21922193
if idx._supports_partial_string_indexing:
21932194
try:
2194-
return idx._get_string_slice(key)
2195+
res = idx._get_string_slice(key)
2196+
warnings.warn(
2197+
"Indexing on datetimelike rows with `frame[string]` is "
2198+
"deprecated and will be removed in a future version. "
2199+
"Use `frame.loc[string]` instead.",
2200+
FutureWarning,
2201+
stacklevel=3,
2202+
)
2203+
return res
21952204
except (KeyError, ValueError, NotImplementedError):
21962205
return None
21972206

pandas/tests/indexes/datetimes/test_partial_slicing.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,9 @@ def test_partial_slicing_dataframe(self):
228228
tm.assert_series_equal(result, expected)
229229

230230
# Frame should return slice as well
231-
result = df[ts_string]
231+
with tm.assert_produces_warning(FutureWarning):
232+
# GH#36179 deprecated this indexing
233+
result = df[ts_string]
232234
expected = df[theslice]
233235
tm.assert_frame_equal(result, expected)
234236

pandas/tests/series/indexing/test_datetime.py

+9-6
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
"""
2+
Also test support for datetime64[ns] in Series / DataFrame
3+
"""
14
from datetime import datetime, timedelta
25
import re
36

@@ -11,10 +14,6 @@
1114
from pandas import DataFrame, DatetimeIndex, NaT, Series, Timestamp, date_range
1215
import pandas._testing as tm
1316

14-
"""
15-
Also test support for datetime64[ns] in Series / DataFrame
16-
"""
17-
1817

1918
def test_fancy_getitem():
2019
dti = date_range(
@@ -605,7 +604,9 @@ def test_indexing():
605604
expected.name = "A"
606605

607606
df = DataFrame(dict(A=ts))
608-
result = df["2001"]["A"]
607+
with tm.assert_produces_warning(FutureWarning):
608+
# GH#36179 string indexing on rows for DataFrame deprecated
609+
result = df["2001"]["A"]
609610
tm.assert_series_equal(expected, result)
610611

611612
# setting
@@ -615,7 +616,9 @@ def test_indexing():
615616

616617
df.loc["2001", "A"] = 1
617618

618-
result = df["2001"]["A"]
619+
with tm.assert_produces_warning(FutureWarning):
620+
# GH#36179 string indexing on rows for DataFrame deprecated
621+
result = df["2001"]["A"]
619622
tm.assert_series_equal(expected, result)
620623

621624
# GH3546 (not including times on the last day)

0 commit comments

Comments
 (0)