Skip to content

Commit 36d454a

Browse files
authored
DEPR: Remove first and last from DataFrame (#57246)
1 parent 1bb4839 commit 36d454a

File tree

6 files changed

+1
-353
lines changed

6 files changed

+1
-353
lines changed

Diff for: doc/source/reference/frame.rst

-2
Original file line numberDiff line numberDiff line change
@@ -186,11 +186,9 @@ Reindexing / selection / label manipulation
186186
DataFrame.duplicated
187187
DataFrame.equals
188188
DataFrame.filter
189-
DataFrame.first
190189
DataFrame.head
191190
DataFrame.idxmax
192191
DataFrame.idxmin
193-
DataFrame.last
194192
DataFrame.reindex
195193
DataFrame.reindex_like
196194
DataFrame.rename

Diff for: doc/source/reference/series.rst

-2
Original file line numberDiff line numberDiff line change
@@ -183,12 +183,10 @@ Reindexing / selection / label manipulation
183183
Series.drop_duplicates
184184
Series.duplicated
185185
Series.equals
186-
Series.first
187186
Series.head
188187
Series.idxmax
189188
Series.idxmin
190189
Series.isin
191-
Series.last
192190
Series.reindex
193191
Series.reindex_like
194192
Series.rename

Diff for: doc/source/whatsnew/v3.0.0.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -102,13 +102,13 @@ Deprecations
102102

103103
Removal of prior version deprecations/changes
104104
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
105+
- Removed :meth:`DataFrame.first` and :meth:`DataFrame.last` (:issue:`53710`)
105106
- Removed :meth:`DataFrameGroupby.fillna` and :meth:`SeriesGroupBy.fillna` (:issue:`55719`)
106107
- Removed ``DataFrameGroupBy.grouper`` and ``SeriesGroupBy.grouper`` (:issue:`56521`)
107108
- Removed ``axis`` argument from :meth:`DataFrame.groupby`, :meth:`Series.groupby`, :meth:`DataFrame.rolling`, :meth:`Series.rolling`, :meth:`DataFrame.resample`, and :meth:`Series.resample` (:issue:`51203`)
108109
- Removed ``axis`` argument from all groupby operations (:issue:`50405`)
109110
- Removed deprecated argument ``obj`` in :meth:`.DataFrameGroupBy.get_group` and :meth:`.SeriesGroupBy.get_group` (:issue:`53545`)
110111
- Removed the ``ArrayManager`` (:issue:`55043`)
111-
-
112112

113113
.. ---------------------------------------------------------------------------
114114
.. _whatsnew_300.performance:

Diff for: pandas/core/generic.py

-164
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636
from pandas._libs.lib import is_range_indexer
3737
from pandas._libs.tslibs import (
3838
Period,
39-
Tick,
4039
Timestamp,
4140
to_offset,
4241
)
@@ -9646,169 +9645,6 @@ def resample(
96469645
group_keys=group_keys,
96479646
)
96489647

9649-
@final
9650-
def first(self, offset) -> Self:
9651-
"""
9652-
Select initial periods of time series data based on a date offset.
9653-
9654-
.. deprecated:: 2.1
9655-
:meth:`.first` is deprecated and will be removed in a future version.
9656-
Please create a mask and filter using `.loc` instead.
9657-
9658-
For a DataFrame with a sorted DatetimeIndex, this function can
9659-
select the first few rows based on a date offset.
9660-
9661-
Parameters
9662-
----------
9663-
offset : str, DateOffset or dateutil.relativedelta
9664-
The offset length of the data that will be selected. For instance,
9665-
'1ME' will display all the rows having their index within the first month.
9666-
9667-
Returns
9668-
-------
9669-
Series or DataFrame
9670-
A subset of the caller.
9671-
9672-
Raises
9673-
------
9674-
TypeError
9675-
If the index is not a :class:`DatetimeIndex`
9676-
9677-
See Also
9678-
--------
9679-
last : Select final periods of time series based on a date offset.
9680-
at_time : Select values at a particular time of the day.
9681-
between_time : Select values between particular times of the day.
9682-
9683-
Examples
9684-
--------
9685-
>>> i = pd.date_range('2018-04-09', periods=4, freq='2D')
9686-
>>> ts = pd.DataFrame({'A': [1, 2, 3, 4]}, index=i)
9687-
>>> ts
9688-
A
9689-
2018-04-09 1
9690-
2018-04-11 2
9691-
2018-04-13 3
9692-
2018-04-15 4
9693-
9694-
Get the rows for the first 3 days:
9695-
9696-
>>> ts.first('3D')
9697-
A
9698-
2018-04-09 1
9699-
2018-04-11 2
9700-
9701-
Notice the data for 3 first calendar days were returned, not the first
9702-
3 days observed in the dataset, and therefore data for 2018-04-13 was
9703-
not returned.
9704-
"""
9705-
warnings.warn(
9706-
"first is deprecated and will be removed in a future version. "
9707-
"Please create a mask and filter using `.loc` instead",
9708-
FutureWarning,
9709-
stacklevel=find_stack_level(),
9710-
)
9711-
if not isinstance(self.index, DatetimeIndex):
9712-
raise TypeError("'first' only supports a DatetimeIndex index")
9713-
9714-
if len(self.index) == 0:
9715-
return self.copy(deep=False)
9716-
9717-
offset = to_offset(offset)
9718-
if not isinstance(offset, Tick) and offset.is_on_offset(self.index[0]):
9719-
# GH#29623 if first value is end of period, remove offset with n = 1
9720-
# before adding the real offset
9721-
end_date = end = self.index[0] - offset.base + offset
9722-
else:
9723-
end_date = end = self.index[0] + offset
9724-
9725-
# Tick-like, e.g. 3 weeks
9726-
if isinstance(offset, Tick) and end_date in self.index:
9727-
end = self.index.searchsorted(end_date, side="left")
9728-
return self.iloc[:end]
9729-
9730-
return self.loc[:end]
9731-
9732-
@final
9733-
def last(self, offset) -> Self:
9734-
"""
9735-
Select final periods of time series data based on a date offset.
9736-
9737-
.. deprecated:: 2.1
9738-
:meth:`.last` is deprecated and will be removed in a future version.
9739-
Please create a mask and filter using `.loc` instead.
9740-
9741-
For a DataFrame with a sorted DatetimeIndex, this function
9742-
selects the last few rows based on a date offset.
9743-
9744-
Parameters
9745-
----------
9746-
offset : str, DateOffset, dateutil.relativedelta
9747-
The offset length of the data that will be selected. For instance,
9748-
'3D' will display all the rows having their index within the last 3 days.
9749-
9750-
Returns
9751-
-------
9752-
Series or DataFrame
9753-
A subset of the caller.
9754-
9755-
Raises
9756-
------
9757-
TypeError
9758-
If the index is not a :class:`DatetimeIndex`
9759-
9760-
See Also
9761-
--------
9762-
first : Select initial periods of time series based on a date offset.
9763-
at_time : Select values at a particular time of the day.
9764-
between_time : Select values between particular times of the day.
9765-
9766-
Notes
9767-
-----
9768-
.. deprecated:: 2.1.0
9769-
Please create a mask and filter using `.loc` instead
9770-
9771-
Examples
9772-
--------
9773-
>>> i = pd.date_range('2018-04-09', periods=4, freq='2D')
9774-
>>> ts = pd.DataFrame({'A': [1, 2, 3, 4]}, index=i)
9775-
>>> ts
9776-
A
9777-
2018-04-09 1
9778-
2018-04-11 2
9779-
2018-04-13 3
9780-
2018-04-15 4
9781-
9782-
Get the rows for the last 3 days:
9783-
9784-
>>> ts.last('3D') # doctest: +SKIP
9785-
A
9786-
2018-04-13 3
9787-
2018-04-15 4
9788-
9789-
Notice the data for 3 last calendar days were returned, not the last
9790-
3 observed days in the dataset, and therefore data for 2018-04-11 was
9791-
not returned.
9792-
"""
9793-
warnings.warn(
9794-
"last is deprecated and will be removed in a future version. "
9795-
"Please create a mask and filter using `.loc` instead",
9796-
FutureWarning,
9797-
stacklevel=find_stack_level(),
9798-
)
9799-
9800-
if not isinstance(self.index, DatetimeIndex):
9801-
raise TypeError("'last' only supports a DatetimeIndex index")
9802-
9803-
if len(self.index) == 0:
9804-
return self.copy(deep=False)
9805-
9806-
offset = to_offset(offset)
9807-
9808-
start_date = self.index[-1] - offset
9809-
start = self.index.searchsorted(start_date, side="right")
9810-
return self.iloc[start:]
9811-
98129648
@final
98139649
def rank(
98149650
self,

Diff for: pandas/tests/frame/methods/test_first_and_last.py

-139
This file was deleted.

0 commit comments

Comments
 (0)