Skip to content

Commit d203ef8

Browse files
DOC: clarify docs related to deprecation of indexing DataFrame with single partial datetime-string (#38088)
1 parent 8b0af58 commit d203ef8

File tree

3 files changed

+15
-17
lines changed

3 files changed

+15
-17
lines changed

doc/source/user_guide/timeseries.rst

+8-13
Original file line numberDiff line numberDiff line change
@@ -588,45 +588,43 @@ would include matching times on an included date:
588588

589589
.. warning::
590590

591-
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.
591+
Indexing ``DataFrame`` rows with a *single* string with getitem (e.g. ``frame[dtstring]``)
592+
is deprecated starting with pandas 1.2.0 (given the ambiguity whether it is indexing
593+
the rows or selecting a column) and will be removed in a future version. The equivalent
594+
with ``.loc`` (e.g. ``frame.loc[dtstring]``) is still supported.
592595

593596
.. ipython:: python
594-
:okwarning:
595597
596598
dft = pd.DataFrame(
597599
np.random.randn(100000, 1),
598600
columns=["A"],
599601
index=pd.date_range("20130101", periods=100000, freq="T"),
600602
)
601603
dft
602-
dft["2013"]
604+
dft.loc["2013"]
603605
604606
This starts on the very first time in the month, and includes the last date and
605607
time for the month:
606608

607609
.. ipython:: python
608-
:okwarning:
609610
610611
dft["2013-1":"2013-2"]
611612
612613
This specifies a stop time **that includes all of the times on the last day**:
613614

614615
.. ipython:: python
615-
:okwarning:
616616
617617
dft["2013-1":"2013-2-28"]
618618
619619
This specifies an **exact** stop time (and is not the same as the above):
620620

621621
.. ipython:: python
622-
:okwarning:
623622
624623
dft["2013-1":"2013-2-28 00:00:00"]
625624
626625
We are stopping on the included end-point as it is part of the index:
627626

628627
.. ipython:: python
629-
:okwarning:
630628
631629
dft["2013-1-15":"2013-1-15 12:30:00"]
632630
@@ -652,7 +650,6 @@ We are stopping on the included end-point as it is part of the index:
652650
Slicing with string indexing also honors UTC offset.
653651

654652
.. ipython:: python
655-
:okwarning:
656653
657654
df = pd.DataFrame([0], index=pd.DatetimeIndex(["2019-01-01"], tz="US/Pacific"))
658655
df
@@ -704,15 +701,14 @@ If index resolution is second, then the minute-accurate timestamp gives a
704701
series_second.index.resolution
705702
series_second["2011-12-31 23:59"]
706703
707-
If the timestamp string is treated as a slice, it can be used to index ``DataFrame`` with ``[]`` as well.
704+
If the timestamp string is treated as a slice, it can be used to index ``DataFrame`` with ``.loc[]`` as well.
708705

709706
.. ipython:: python
710-
:okwarning:
711707
712708
dft_minute = pd.DataFrame(
713709
{"a": [1, 2, 3], "b": [4, 5, 6]}, index=series_minute.index
714710
)
715-
dft_minute["2011-12-31 23"]
711+
dft_minute.loc["2011-12-31 23"]
716712
717713
718714
.. warning::
@@ -2080,7 +2076,6 @@ You can pass in dates and strings to ``Series`` and ``DataFrame`` with ``PeriodI
20802076
Passing a string representing a lower frequency than ``PeriodIndex`` returns partial sliced data.
20812077

20822078
.. ipython:: python
2083-
:okwarning:
20842079
20852080
ps["2011"]
20862081
@@ -2090,7 +2085,7 @@ Passing a string representing a lower frequency than ``PeriodIndex`` returns par
20902085
index=pd.period_range("2013-01-01 9:00", periods=600, freq="T"),
20912086
)
20922087
dfp
2093-
dfp["2013-01-01 10H"]
2088+
dfp.loc["2013-01-01 10H"]
20942089
20952090
As with ``DatetimeIndex``, the endpoints will be included in the result. The example below slices data starting from 10:00 to 11:59.
20962091

doc/source/whatsnew/v1.2.0.rst

+3-1
Original file line numberDiff line numberDiff line change
@@ -467,7 +467,9 @@ Deprecations
467467
- 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`)
468468
- :meth:`DataFrame.lookup` is deprecated and will be removed in a future version, use :meth:`DataFrame.melt` and :meth:`DataFrame.loc` instead (:issue:`18682`)
469469
- The method :meth:`Index.to_native_types` is deprecated. Use ``.astype(str)`` instead (:issue:`28867`)
470-
- Deprecated indexing :class:`DataFrame` rows with datetime-like strings ``df[string]``, use ``df.loc[string]`` instead (:issue:`36179`)
470+
- Deprecated indexing :class:`DataFrame` rows with a single datetime-like string as ``df[string]``
471+
(given the ambiguity whether it is indexing the rows or selecting a column), use
472+
``df.loc[string]`` instead (:issue:`36179`)
471473
- Deprecated casting an object-dtype index of ``datetime`` objects to :class:`.DatetimeIndex` in the :class:`Series` constructor (:issue:`23598`)
472474
- Deprecated :meth:`Index.is_all_dates` (:issue:`27744`)
473475
- The default value of ``regex`` for :meth:`Series.str.replace` will change from ``True`` to ``False`` in a future release. In addition, single character regular expressions will *not* be treated as literal strings when ``regex=True`` is set. (:issue:`24804`)

pandas/core/indexing.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -2199,9 +2199,10 @@ def convert_to_index_sliceable(obj: "DataFrame", key):
21992199
try:
22002200
res = idx._get_string_slice(key)
22012201
warnings.warn(
2202-
"Indexing on datetimelike rows with `frame[string]` is "
2203-
"deprecated and will be removed in a future version. "
2204-
"Use `frame.loc[string]` instead.",
2202+
"Indexing a DataFrame with a datetimelike index using a single "
2203+
"string to slice the rows, like `frame[string]`, is deprecated "
2204+
"and will be removed in a future version. Use `frame.loc[string]` "
2205+
"instead.",
22052206
FutureWarning,
22062207
stacklevel=3,
22072208
)

0 commit comments

Comments
 (0)