Skip to content

DEPR: Remove first and last from DataFrame #57246

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions doc/source/reference/frame.rst
Original file line number Diff line number Diff line change
Expand Up @@ -186,11 +186,9 @@ Reindexing / selection / label manipulation
DataFrame.duplicated
DataFrame.equals
DataFrame.filter
DataFrame.first
DataFrame.head
DataFrame.idxmax
DataFrame.idxmin
DataFrame.last
DataFrame.reindex
DataFrame.reindex_like
DataFrame.rename
Expand Down
2 changes: 0 additions & 2 deletions doc/source/reference/series.rst
Original file line number Diff line number Diff line change
Expand Up @@ -183,12 +183,10 @@ Reindexing / selection / label manipulation
Series.drop_duplicates
Series.duplicated
Series.equals
Series.first
Series.head
Series.idxmax
Series.idxmin
Series.isin
Series.last
Series.reindex
Series.reindex_like
Series.rename
Expand Down
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v3.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -102,13 +102,13 @@ Deprecations

Removal of prior version deprecations/changes
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- Removed :meth:`DataFrame.first` and :meth:`DataFrame.last` (:issue:`53710`)
- Removed :meth:`DataFrameGroupby.fillna` and :meth:`SeriesGroupBy.fillna` (:issue:`55719`)
- Removed ``DataFrameGroupBy.grouper`` and ``SeriesGroupBy.grouper`` (:issue:`56521`)
- 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`)
- Removed ``axis`` argument from all groupby operations (:issue:`50405`)
- Removed deprecated argument ``obj`` in :meth:`.DataFrameGroupBy.get_group` and :meth:`.SeriesGroupBy.get_group` (:issue:`53545`)
- Removed the ``ArrayManager`` (:issue:`55043`)
-

.. ---------------------------------------------------------------------------
.. _whatsnew_300.performance:
Expand Down
164 changes: 0 additions & 164 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
from pandas._libs.lib import is_range_indexer
from pandas._libs.tslibs import (
Period,
Tick,
Timestamp,
to_offset,
)
Expand Down Expand Up @@ -9646,169 +9645,6 @@ def resample(
group_keys=group_keys,
)

@final
def first(self, offset) -> Self:
"""
Select initial periods of time series data based on a date offset.

.. deprecated:: 2.1
:meth:`.first` is deprecated and will be removed in a future version.
Please create a mask and filter using `.loc` instead.

For a DataFrame with a sorted DatetimeIndex, this function can
select the first few rows based on a date offset.

Parameters
----------
offset : str, DateOffset or dateutil.relativedelta
The offset length of the data that will be selected. For instance,
'1ME' will display all the rows having their index within the first month.

Returns
-------
Series or DataFrame
A subset of the caller.

Raises
------
TypeError
If the index is not a :class:`DatetimeIndex`

See Also
--------
last : Select final periods of time series based on a date offset.
at_time : Select values at a particular time of the day.
between_time : Select values between particular times of the day.

Examples
--------
>>> i = pd.date_range('2018-04-09', periods=4, freq='2D')
>>> ts = pd.DataFrame({'A': [1, 2, 3, 4]}, index=i)
>>> ts
A
2018-04-09 1
2018-04-11 2
2018-04-13 3
2018-04-15 4

Get the rows for the first 3 days:

>>> ts.first('3D')
A
2018-04-09 1
2018-04-11 2

Notice the data for 3 first calendar days were returned, not the first
3 days observed in the dataset, and therefore data for 2018-04-13 was
not returned.
"""
warnings.warn(
"first is deprecated and will be removed in a future version. "
"Please create a mask and filter using `.loc` instead",
FutureWarning,
stacklevel=find_stack_level(),
)
if not isinstance(self.index, DatetimeIndex):
raise TypeError("'first' only supports a DatetimeIndex index")

if len(self.index) == 0:
return self.copy(deep=False)

offset = to_offset(offset)
if not isinstance(offset, Tick) and offset.is_on_offset(self.index[0]):
# GH#29623 if first value is end of period, remove offset with n = 1
# before adding the real offset
end_date = end = self.index[0] - offset.base + offset
else:
end_date = end = self.index[0] + offset

# Tick-like, e.g. 3 weeks
if isinstance(offset, Tick) and end_date in self.index:
end = self.index.searchsorted(end_date, side="left")
return self.iloc[:end]

return self.loc[:end]

@final
def last(self, offset) -> Self:
"""
Select final periods of time series data based on a date offset.

.. deprecated:: 2.1
:meth:`.last` is deprecated and will be removed in a future version.
Please create a mask and filter using `.loc` instead.

For a DataFrame with a sorted DatetimeIndex, this function
selects the last few rows based on a date offset.

Parameters
----------
offset : str, DateOffset, dateutil.relativedelta
The offset length of the data that will be selected. For instance,
'3D' will display all the rows having their index within the last 3 days.

Returns
-------
Series or DataFrame
A subset of the caller.

Raises
------
TypeError
If the index is not a :class:`DatetimeIndex`

See Also
--------
first : Select initial periods of time series based on a date offset.
at_time : Select values at a particular time of the day.
between_time : Select values between particular times of the day.

Notes
-----
.. deprecated:: 2.1.0
Please create a mask and filter using `.loc` instead

Examples
--------
>>> i = pd.date_range('2018-04-09', periods=4, freq='2D')
>>> ts = pd.DataFrame({'A': [1, 2, 3, 4]}, index=i)
>>> ts
A
2018-04-09 1
2018-04-11 2
2018-04-13 3
2018-04-15 4

Get the rows for the last 3 days:

>>> ts.last('3D') # doctest: +SKIP
A
2018-04-13 3
2018-04-15 4

Notice the data for 3 last calendar days were returned, not the last
3 observed days in the dataset, and therefore data for 2018-04-11 was
not returned.
"""
warnings.warn(
"last is deprecated and will be removed in a future version. "
"Please create a mask and filter using `.loc` instead",
FutureWarning,
stacklevel=find_stack_level(),
)

if not isinstance(self.index, DatetimeIndex):
raise TypeError("'last' only supports a DatetimeIndex index")

if len(self.index) == 0:
return self.copy(deep=False)

offset = to_offset(offset)

start_date = self.index[-1] - offset
start = self.index.searchsorted(start_date, side="right")
return self.iloc[start:]

@final
def rank(
self,
Expand Down
139 changes: 0 additions & 139 deletions pandas/tests/frame/methods/test_first_and_last.py

This file was deleted.

Loading