title stringlengths 1 185 | diff stringlengths 0 32.2M | body stringlengths 0 123k ⌀ | url stringlengths 57 58 | created_at stringlengths 20 20 | closed_at stringlengths 20 20 | merged_at stringlengths 20 20 ⌀ | updated_at stringlengths 20 20 |
|---|---|---|---|---|---|---|---|
TYP: misc IO return types | diff --git a/pandas/_typing.py b/pandas/_typing.py
index c704516f74300..1fec41463904c 100644
--- a/pandas/_typing.py
+++ b/pandas/_typing.py
@@ -529,3 +529,6 @@ def closed(self) -> bool:
Callable[[HashableT], bool],
None,
]
+
+# maintaine the sub-type of any hashable sequence
+SequenceT = TypeVar("SequenceT", bound=Sequence[Hashable])
diff --git a/pandas/io/excel/_openpyxl.py b/pandas/io/excel/_openpyxl.py
index c546443868a62..218a592c22b4a 100644
--- a/pandas/io/excel/_openpyxl.py
+++ b/pandas/io/excel/_openpyxl.py
@@ -26,6 +26,7 @@
if TYPE_CHECKING:
from openpyxl import Workbook
from openpyxl.descriptors.serialisable import Serialisable
+ from openpyxl.styles import Fill
from pandas._typing import (
ExcelWriterIfSheetExists,
@@ -244,7 +245,7 @@ def _convert_to_stop(cls, stop_seq):
return map(cls._convert_to_color, stop_seq)
@classmethod
- def _convert_to_fill(cls, fill_dict: dict[str, Any]):
+ def _convert_to_fill(cls, fill_dict: dict[str, Any]) -> Fill:
"""
Convert ``fill_dict`` to an openpyxl v2 Fill object.
diff --git a/pandas/io/formats/excel.py b/pandas/io/formats/excel.py
index 0c3a53eb1cfea..892f69e76359b 100644
--- a/pandas/io/formats/excel.py
+++ b/pandas/io/formats/excel.py
@@ -284,7 +284,9 @@ def build_border(
for side in ["top", "right", "bottom", "left"]
}
- def _border_style(self, style: str | None, width: str | None, color: str | None):
+ def _border_style(
+ self, style: str | None, width: str | None, color: str | None
+ ) -> str | None:
# convert styles and widths to openxml, one of:
# 'dashDot'
# 'dashDotDot'
diff --git a/pandas/io/formats/format.py b/pandas/io/formats/format.py
index 00c7526edfa48..65124f97459cd 100644
--- a/pandas/io/formats/format.py
+++ b/pandas/io/formats/format.py
@@ -1346,7 +1346,9 @@ def get_result_as_array(self) -> np.ndarray:
the parameters given at initialisation, as a numpy array
"""
- def format_with_na_rep(values: ArrayLike, formatter: Callable, na_rep: str):
+ def format_with_na_rep(
+ values: ArrayLike, formatter: Callable, na_rep: str
+ ) -> np.ndarray:
mask = isna(values)
formatted = np.array(
[
@@ -1358,7 +1360,7 @@ def format_with_na_rep(values: ArrayLike, formatter: Callable, na_rep: str):
def format_complex_with_na_rep(
values: ArrayLike, formatter: Callable, na_rep: str
- ):
+ ) -> np.ndarray:
real_values = np.real(values).ravel() # type: ignore[arg-type]
imag_values = np.imag(values).ravel() # type: ignore[arg-type]
real_mask, imag_mask = isna(real_values), isna(imag_values)
diff --git a/pandas/io/formats/info.py b/pandas/io/formats/info.py
index 552affbd053f2..2d28b032ca49d 100644
--- a/pandas/io/formats/info.py
+++ b/pandas/io/formats/info.py
@@ -392,7 +392,7 @@ def dtype_counts(self) -> Mapping[str, int]:
@property
@abstractmethod
- def non_null_counts(self) -> Sequence[int]:
+ def non_null_counts(self) -> list[int] | Series:
"""Sequence of non-null counts for all columns or column (if series)."""
@property
@@ -486,7 +486,7 @@ def col_count(self) -> int:
return len(self.ids)
@property
- def non_null_counts(self) -> Sequence[int]:
+ def non_null_counts(self) -> Series:
"""Sequence of non-null counts for all columns or column (if series)."""
return self.data.count()
@@ -546,7 +546,7 @@ def render(
printer.to_buffer(buf)
@property
- def non_null_counts(self) -> Sequence[int]:
+ def non_null_counts(self) -> list[int]:
return [self.data.count()]
@property
@@ -750,7 +750,7 @@ def memory_usage_string(self) -> str:
return self.info.memory_usage_string
@property
- def non_null_counts(self) -> Sequence[int]:
+ def non_null_counts(self) -> list[int] | Series:
return self.info.non_null_counts
def add_object_type_line(self) -> None:
diff --git a/pandas/io/formats/style.py b/pandas/io/formats/style.py
index c85c6c3ef0ff7..0e67949709a22 100644
--- a/pandas/io/formats/style.py
+++ b/pandas/io/formats/style.py
@@ -3827,7 +3827,7 @@ def _background_gradient(
vmax: float | None = None,
gmap: Sequence | np.ndarray | DataFrame | Series | None = None,
text_only: bool = False,
-):
+) -> list[str] | DataFrame:
"""
Color background in a range according to the data or a gradient map
"""
diff --git a/pandas/io/formats/style_render.py b/pandas/io/formats/style_render.py
index 80df46bf2336a..4ba094ec614d0 100644
--- a/pandas/io/formats/style_render.py
+++ b/pandas/io/formats/style_render.py
@@ -2030,7 +2030,9 @@ def _class_styles(self):
}
]
- def _pseudo_css(self, uuid: str, name: str, row: int, col: int, text: str):
+ def _pseudo_css(
+ self, uuid: str, name: str, row: int, col: int, text: str
+ ) -> list[CSSDict]:
"""
For every table data-cell that has a valid tooltip (not None, NaN or
empty string) must create two pseudo CSS entries for the specific
diff --git a/pandas/io/html.py b/pandas/io/html.py
index 0f3704b698915..302f901aa0d16 100644
--- a/pandas/io/html.py
+++ b/pandas/io/html.py
@@ -469,7 +469,7 @@ def row_is_all_th(row):
def _expand_colspan_rowspan(
self, rows, section: Literal["header", "footer", "body"]
- ):
+ ) -> list[list]:
"""
Given a list of <tr>s, return a list of text rows.
diff --git a/pandas/io/json/_normalize.py b/pandas/io/json/_normalize.py
index 39fbce0b6901c..49f95430d9bb9 100644
--- a/pandas/io/json/_normalize.py
+++ b/pandas/io/json/_normalize.py
@@ -11,6 +11,7 @@
TYPE_CHECKING,
Any,
DefaultDict,
+ overload,
)
import numpy as np
@@ -42,13 +43,35 @@ def convert_to_line_delimits(s: str) -> str:
return convert_json_to_lines(s)
+@overload
def nested_to_record(
- ds,
+ ds: dict,
+ prefix: str = ...,
+ sep: str = ...,
+ level: int = ...,
+ max_level: int | None = ...,
+) -> dict[str, Any]:
+ ...
+
+
+@overload
+def nested_to_record(
+ ds: list[dict],
+ prefix: str = ...,
+ sep: str = ...,
+ level: int = ...,
+ max_level: int | None = ...,
+) -> list[dict[str, Any]]:
+ ...
+
+
+def nested_to_record(
+ ds: dict | list[dict],
prefix: str = "",
sep: str = ".",
level: int = 0,
max_level: int | None = None,
-):
+) -> dict[str, Any] | list[dict[str, Any]]:
"""
A simplified json_normalize
diff --git a/pandas/io/parquet.py b/pandas/io/parquet.py
index e04a3acc829f1..a6d58d6cffb10 100644
--- a/pandas/io/parquet.py
+++ b/pandas/io/parquet.py
@@ -150,7 +150,7 @@ def validate_dataframe(df: DataFrame) -> None:
if not isinstance(df, DataFrame):
raise ValueError("to_parquet only supports IO with DataFrames")
- def write(self, df: DataFrame, path, compression, **kwargs):
+ def write(self, df: DataFrame, path, compression, **kwargs) -> None:
raise AbstractMethodError(self)
def read(self, path, columns=None, **kwargs) -> DataFrame:
diff --git a/pandas/io/parsers/base_parser.py b/pandas/io/parsers/base_parser.py
index 09f0f2af8e5c6..3aef0692d5f59 100644
--- a/pandas/io/parsers/base_parser.py
+++ b/pandas/io/parsers/base_parser.py
@@ -84,7 +84,6 @@
if TYPE_CHECKING:
from collections.abc import (
- Hashable,
Iterable,
Mapping,
Sequence,
@@ -94,7 +93,10 @@
ArrayLike,
DtypeArg,
DtypeObj,
+ Hashable,
+ HashableT,
Scalar,
+ SequenceT,
)
@@ -350,13 +352,13 @@ def extract(r):
@final
def _maybe_make_multi_index_columns(
self,
- columns: Sequence[Hashable],
+ columns: SequenceT,
col_names: Sequence[Hashable] | None = None,
- ) -> Sequence[Hashable] | MultiIndex:
+ ) -> SequenceT | MultiIndex:
# possibly create a column mi here
if is_potential_multi_index(columns):
- list_columns = cast(list[tuple], columns)
- return MultiIndex.from_tuples(list_columns, names=col_names)
+ columns_mi = cast("Sequence[tuple[Hashable, ...]]", columns)
+ return MultiIndex.from_tuples(columns_mi, names=col_names)
return columns
@final
@@ -520,7 +522,7 @@ def _convert_to_ndarrays(
verbose: bool = False,
converters=None,
dtypes=None,
- ):
+ ) -> dict[Any, np.ndarray]:
result = {}
for c, values in dct.items():
conv_f = None if converters is None else converters.get(c, None)
@@ -923,23 +925,23 @@ def _check_data_length(
@overload
def _evaluate_usecols(
self,
- usecols: set[int] | Callable[[Hashable], object],
- names: Sequence[Hashable],
+ usecols: Callable[[Hashable], object],
+ names: Iterable[Hashable],
) -> set[int]:
...
@overload
def _evaluate_usecols(
- self, usecols: set[str], names: Sequence[Hashable]
- ) -> set[str]:
+ self, usecols: SequenceT, names: Iterable[Hashable]
+ ) -> SequenceT:
...
@final
def _evaluate_usecols(
self,
- usecols: Callable[[Hashable], object] | set[str] | set[int],
- names: Sequence[Hashable],
- ) -> set[str] | set[int]:
+ usecols: Callable[[Hashable], object] | SequenceT,
+ names: Iterable[Hashable],
+ ) -> SequenceT | set[int]:
"""
Check whether or not the 'usecols' parameter
is a callable. If so, enumerates the 'names'
@@ -952,7 +954,7 @@ def _evaluate_usecols(
return usecols
@final
- def _validate_usecols_names(self, usecols, names: Sequence):
+ def _validate_usecols_names(self, usecols: SequenceT, names: Sequence) -> SequenceT:
"""
Validates that all usecols are present in a given
list of names. If not, raise a ValueError that
@@ -1072,7 +1074,9 @@ def _clean_index_names(self, columns, index_col) -> tuple[list | None, list, lis
return index_names, columns, index_col
@final
- def _get_empty_meta(self, columns, dtype: DtypeArg | None = None):
+ def _get_empty_meta(
+ self, columns: Sequence[HashableT], dtype: DtypeArg | None = None
+ ) -> tuple[Index, list[HashableT], dict[HashableT, Series]]:
columns = list(columns)
index_col = self.index_col
@@ -1275,7 +1279,7 @@ def _process_date_conversion(
columns,
keep_date_col: bool = False,
dtype_backend=lib.no_default,
-):
+) -> tuple[dict, list]:
def _isindex(colspec):
return (isinstance(index_col, list) and colspec in index_col) or (
isinstance(index_names, list) and colspec in index_names
diff --git a/pandas/io/parsers/c_parser_wrapper.py b/pandas/io/parsers/c_parser_wrapper.py
index 0cd788c5e5739..f24d7a628998e 100644
--- a/pandas/io/parsers/c_parser_wrapper.py
+++ b/pandas/io/parsers/c_parser_wrapper.py
@@ -41,10 +41,12 @@
)
from pandas._typing import (
+ AnyArrayLike,
ArrayLike,
DtypeArg,
DtypeObj,
ReadCsvBuffer,
+ SequenceT,
)
from pandas import (
@@ -225,7 +227,7 @@ def read(
) -> tuple[
Index | MultiIndex | None,
Sequence[Hashable] | MultiIndex,
- Mapping[Hashable, ArrayLike],
+ Mapping[Hashable, AnyArrayLike],
]:
index: Index | MultiIndex | None
column_names: Sequence[Hashable] | MultiIndex
@@ -248,7 +250,11 @@ def read(
names,
dtype=self.dtype,
)
- columns = self._maybe_make_multi_index_columns(columns, self.col_names)
+ # error: Incompatible types in assignment (expression has type
+ # "list[Hashable] | MultiIndex", variable has type "list[Hashable]")
+ columns = self._maybe_make_multi_index_columns( # type: ignore[assignment]
+ columns, self.col_names
+ )
if self.usecols is not None:
columns = self._filter_usecols(columns)
@@ -334,11 +340,11 @@ def read(
return index, column_names, date_data
- def _filter_usecols(self, names: Sequence[Hashable]) -> Sequence[Hashable]:
+ def _filter_usecols(self, names: SequenceT) -> SequenceT | list[Hashable]:
# hackish
usecols = self._evaluate_usecols(self.usecols, names)
if usecols is not None and len(names) != len(usecols):
- names = [
+ return [
name for i, name in enumerate(names) if i in usecols or name in usecols
]
return names
diff --git a/pandas/io/parsers/python_parser.py b/pandas/io/parsers/python_parser.py
index e830db559c5a5..dbda47172f6ac 100644
--- a/pandas/io/parsers/python_parser.py
+++ b/pandas/io/parsers/python_parser.py
@@ -266,6 +266,7 @@ def read(
# done with first read, next time raise StopIteration
self._first_chunk = False
+ index: Index | None
columns: Sequence[Hashable] = list(self.orig_names)
if not len(content): # pragma: no cover
# DataFrame with the right metadata, even though it's length 0
diff --git a/pandas/io/parsers/readers.py b/pandas/io/parsers/readers.py
index d35c153459bf8..71e1a31759a0c 100644
--- a/pandas/io/parsers/readers.py
+++ b/pandas/io/parsers/readers.py
@@ -2101,7 +2101,7 @@ def _floatify_na_values(na_values):
return result
-def _stringify_na_values(na_values, floatify: bool):
+def _stringify_na_values(na_values, floatify: bool) -> set[str | float]:
"""return a stringified and numeric for these values"""
result: list[str | float] = []
for x in na_values:
diff --git a/pandas/io/sas/sas7bdat.py b/pandas/io/sas/sas7bdat.py
index 895079bc15588..275fad2a565bf 100644
--- a/pandas/io/sas/sas7bdat.py
+++ b/pandas/io/sas/sas7bdat.py
@@ -54,6 +54,7 @@
from pandas._typing import (
CompressionOptions,
FilePath,
+ NaTType,
ReadBuffer,
)
@@ -62,7 +63,7 @@
_sas_origin = Timestamp("1960-01-01")
-def _parse_datetime(sas_datetime: float, unit: str):
+def _parse_datetime(sas_datetime: float, unit: str) -> datetime | NaTType:
if isna(sas_datetime):
return pd.NaT
@@ -326,7 +327,7 @@ def __next__(self) -> DataFrame:
return da
# Read a single float of the given width (4 or 8).
- def _read_float(self, offset: int, width: int):
+ def _read_float(self, offset: int, width: int) -> float:
assert self._cached_page is not None
if width == 4:
return read_float_with_byteswap(
diff --git a/pandas/io/sql.py b/pandas/io/sql.py
index d9a5e6dfd0cf8..4e0ddd0f56ba8 100644
--- a/pandas/io/sql.py
+++ b/pandas/io/sql.py
@@ -67,6 +67,7 @@
if TYPE_CHECKING:
from collections.abc import (
+ Generator,
Iterator,
Mapping,
)
@@ -136,7 +137,7 @@ def _handle_date_column(
return to_datetime(col, errors="coerce", format=format, utc=utc)
-def _parse_date_columns(data_frame, parse_dates):
+def _parse_date_columns(data_frame: DataFrame, parse_dates) -> DataFrame:
"""
Force non-datetime columns to be read as such.
Supports both string formatted and integer timestamp columns.
@@ -199,7 +200,7 @@ def _wrap_result(
parse_dates=None,
dtype: DtypeArg | None = None,
dtype_backend: DtypeBackend | Literal["numpy"] = "numpy",
-):
+) -> DataFrame:
"""Wrap result set of a SQLAlchemy query in a DataFrame."""
frame = _convert_arrays_to_dataframe(data, columns, coerce_float, dtype_backend)
@@ -1153,7 +1154,7 @@ def _query_iterator(
coerce_float: bool = True,
parse_dates=None,
dtype_backend: DtypeBackend | Literal["numpy"] = "numpy",
- ):
+ ) -> Generator[DataFrame, None, None]:
"""Return generator through chunked result set."""
has_read_data = False
with exit_stack:
@@ -1765,7 +1766,7 @@ def _query_iterator(
parse_dates=None,
dtype: DtypeArg | None = None,
dtype_backend: DtypeBackend | Literal["numpy"] = "numpy",
- ):
+ ) -> Generator[DataFrame, None, None]:
"""Return generator through chunked result set"""
has_read_data = False
with exit_stack:
@@ -2466,7 +2467,7 @@ def _create_sql_schema(
}
-def _get_unicode_name(name: object):
+def _get_unicode_name(name: object) -> str:
try:
uname = str(name).encode("utf-8", "strict").decode("utf-8")
except UnicodeError as err:
@@ -2474,7 +2475,7 @@ def _get_unicode_name(name: object):
return uname
-def _get_valid_sqlite_name(name: object):
+def _get_valid_sqlite_name(name: object) -> str:
# See https://stackoverflow.com/questions/6514274/how-do-you-escape-strings\
# -for-sqlite-table-column-names-in-python
# Ensure the string can be encoded as UTF-8.
@@ -2712,7 +2713,7 @@ def _query_iterator(
parse_dates=None,
dtype: DtypeArg | None = None,
dtype_backend: DtypeBackend | Literal["numpy"] = "numpy",
- ):
+ ) -> Generator[DataFrame, None, None]:
"""Return generator through chunked result set"""
has_read_data = False
while True:
diff --git a/pandas/io/stata.py b/pandas/io/stata.py
index a2c15938c04bf..447c97d078e02 100644
--- a/pandas/io/stata.py
+++ b/pandas/io/stata.py
@@ -373,7 +373,7 @@ def _datetime_to_stata_elapsed_vec(dates: Series, fmt: str) -> Series:
def parse_dates_safe(
dates: Series, delta: bool = False, year: bool = False, days: bool = False
- ):
+ ) -> DataFrame:
d = {}
if lib.is_np_dtype(dates.dtype, "M"):
if delta:
| mostly for the CSV methods | https://api.github.com/repos/pandas-dev/pandas/pulls/57228 | 2024-02-03T19:09:18Z | 2024-02-03T21:24:50Z | 2024-02-03T21:24:50Z | 2024-02-10T20:34:31Z |
Update style.ipynb http -> https | diff --git a/doc/source/user_guide/style.ipynb b/doc/source/user_guide/style.ipynb
index f22a506499cf4..2d4b0f6a7545e 100644
--- a/doc/source/user_guide/style.ipynb
+++ b/doc/source/user_guide/style.ipynb
@@ -1280,7 +1280,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
- "You can create \"heatmaps\" with the `background_gradient` and `text_gradient` methods. These require matplotlib, and we'll use [Seaborn](http://seaborn.pydata.org/) to get a nice colormap."
+ "You can create \"heatmaps\" with the `background_gradient` and `text_gradient` methods. These require matplotlib, and we'll use [Seaborn](https://seaborn.pydata.org/) to get a nice colormap."
]
},
{
| http://seaborn.pydata.org/ -> https://seaborn.pydata.org/
- [ ] closes #xxxx (Replace xxxx with the GitHub issue number)
- [ ] [Tests added and passed](https://pandas.pydata.org/pandas-docs/dev/development/contributing_codebase.html#writing-tests) if fixing a bug or adding a new feature
- [ ] All [code checks passed](https://pandas.pydata.org/pandas-docs/dev/development/contributing_codebase.html#pre-commit).
- [ ] Added [type annotations](https://pandas.pydata.org/pandas-docs/dev/development/contributing_codebase.html#type-hints) to new arguments/methods/functions.
- [ ] Added an entry in the latest `doc/source/whatsnew/vX.X.X.rst` file if fixing a bug or adding a new feature.
| https://api.github.com/repos/pandas-dev/pandas/pulls/57227 | 2024-02-03T18:25:44Z | 2024-02-03T20:01:01Z | 2024-02-03T20:01:01Z | 2024-02-03T21:38:54Z |
WEB: Using Bootstrap icon instead of font awesome icons | diff --git a/web/pandas/_templates/layout.html b/web/pandas/_templates/layout.html
index c8025aeef3791..aa4bfc92ce8a8 100644
--- a/web/pandas/_templates/layout.html
+++ b/web/pandas/_templates/layout.html
@@ -14,7 +14,7 @@
<link rel="stylesheet"
href="{{ base_url }}{{ stylesheet }}">
{% endfor %}
- <script src="https://kit.fontawesome.com/79e5369384.js" crossorigin="anonymous"></script>
+ <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.3/font/bootstrap-icons.min.css">
</head>
<body>
<header>
@@ -64,27 +64,27 @@
<ul class="list-inline social-buttons float-end">
<li class="list-inline-item">
<a href="https://t.me/s/pandas_dev">
- <i class="fab fa-telegram"></i>
+ <i class="fab bi bi-telegram"></i>
</a>
</li>
<li class="list-inline-item">
<a href="https://fosstodon.org/@pandas_dev" rel="me">
- <i class="fab fa-mastodon"></i>
+ <i class="fab bi bi-mastodon"></i>
</a>
</li>
<li class="list-inline-item">
<a href="https://twitter.com/pandas_dev/">
- <i class="fab fa-twitter"></i>
+ <i class="fab bi bi-twitter"></i>
</a>
</li>
<li class="list-inline-item">
<a href="https://github.com/pandas-dev/pandas/">
- <i class="fab fa-github"></i>
+ <i class="fab bi bi-github"></i>
</a>
</li>
<li class="list-inline-item">
<a href="https://stackoverflow.com/questions/tagged/pandas">
- <i class="fab fa-stack-overflow"></i>
+ <i class="fab bi bi-stack-overflow"></i>
</a>
</li>
</ul>
diff --git a/web/pandas/index.html b/web/pandas/index.html
index 3d2e5363a2a2d..a9f5c35458bc8 100644
--- a/web/pandas/index.html
+++ b/web/pandas/index.html
@@ -74,17 +74,17 @@ <h4>Follow us</h4>
<ul class="list-inline social-buttons">
<li class="list-inline-item">
<a href="https://t.me/s/pandas_dev">
- <i class="follow-us-button fab fa-telegram"></i>
+ <i class="follow-us-button fab bi bi-telegram"></i>
</a>
</li>
<li class="list-inline-item">
<a href="https://fosstodon.org/@pandas_dev" rel="me">
- <i class="follow-us-button fab fa-mastodon"></i>
+ <i class="follow-us-button fab bi bi-mastodon"></i>
</a>
</li>
<li class="list-inline-item">
<a href="https://twitter.com/pandas_dev/">
- <i class="follow-us-button fab fa-twitter"></i>
+ <i class="follow-us-button fab bi bi-twitter"></i>
</a>
</li>
</ul>
| Closes https://github.com/pandas-dev/pandas/issues/57165
| https://api.github.com/repos/pandas-dev/pandas/pulls/57226 | 2024-02-03T18:11:13Z | 2024-02-05T17:32:53Z | 2024-02-05T17:32:53Z | 2024-02-05T17:33:00Z |
BUG: Raise if an aggregation function other than mean is used with ewm and times | diff --git a/doc/source/whatsnew/v3.0.0.rst b/doc/source/whatsnew/v3.0.0.rst
index f9117253b61c1..7c74a2698093d 100644
--- a/doc/source/whatsnew/v3.0.0.rst
+++ b/doc/source/whatsnew/v3.0.0.rst
@@ -205,7 +205,7 @@ Plotting
Groupby/resample/rolling
^^^^^^^^^^^^^^^^^^^^^^^^
- Bug in :meth:`.DataFrameGroupBy.quantile` when ``interpolation="nearest"`` is inconsistent with :meth:`DataFrame.quantile` (:issue:`47942`)
--
+- Bug in :meth:`DataFrame.ewm` and :meth:`Series.ewm` when passed ``times`` and aggregation functions other than mean (:issue:`51695`)
Reshaping
^^^^^^^^^
diff --git a/pandas/core/window/ewm.py b/pandas/core/window/ewm.py
index 3c07fc156aea1..01d1787d46ca0 100644
--- a/pandas/core/window/ewm.py
+++ b/pandas/core/window/ewm.py
@@ -587,6 +587,8 @@ def sum(
):
if not self.adjust:
raise NotImplementedError("sum is not implemented with adjust=False")
+ if self.times is not None:
+ raise NotImplementedError("sum is not implemented with times")
if maybe_use_numba(engine):
if self.method == "single":
func = generate_numba_ewm_func
@@ -658,6 +660,8 @@ def std(self, bias: bool = False, numeric_only: bool = False):
raise NotImplementedError(
f"{type(self).__name__}.std does not implement numeric_only"
)
+ if self.times is not None:
+ raise NotImplementedError("std is not implemented with times")
return zsqrt(self.var(bias=bias, numeric_only=numeric_only))
@doc(
@@ -691,6 +695,8 @@ def std(self, bias: bool = False, numeric_only: bool = False):
agg_method="var",
)
def var(self, bias: bool = False, numeric_only: bool = False):
+ if self.times is not None:
+ raise NotImplementedError("var is not implemented with times")
window_func = window_aggregations.ewmcov
wfunc = partial(
window_func,
@@ -753,6 +759,9 @@ def cov(
bias: bool = False,
numeric_only: bool = False,
):
+ if self.times is not None:
+ raise NotImplementedError("cov is not implemented with times")
+
from pandas import Series
self._validate_numeric_only("cov", numeric_only)
@@ -837,6 +846,9 @@ def corr(
pairwise: bool | None = None,
numeric_only: bool = False,
):
+ if self.times is not None:
+ raise NotImplementedError("corr is not implemented with times")
+
from pandas import Series
self._validate_numeric_only("corr", numeric_only)
diff --git a/pandas/tests/window/test_ewm.py b/pandas/tests/window/test_ewm.py
index 2e2cfa156019f..35c896dc0090b 100644
--- a/pandas/tests/window/test_ewm.py
+++ b/pandas/tests/window/test_ewm.py
@@ -173,6 +173,18 @@ def test_ewm_sum_adjust_false_notimplemented():
data.sum()
+@pytest.mark.parametrize("method", ["sum", "std", "var", "cov", "corr"])
+def test_times_only_mean_implemented(frame_or_series, method):
+ # GH 51695
+ halflife = "1 day"
+ times = date_range("2000", freq="D", periods=10)
+ ewm = frame_or_series(range(10)).ewm(halflife=halflife, times=times)
+ with pytest.raises(
+ NotImplementedError, match=f"{method} is not implemented with times"
+ ):
+ getattr(ewm, method)()
+
+
@pytest.mark.parametrize(
"expected_data, ignore",
[[[10.0, 5.0, 2.5, 11.25], False], [[10.0, 5.0, 5.0, 12.5], True]],
| Ran into this recently and found an issue was reported a while ago without any progress (#51695)
- [x] closes #51695
- [x] [Tests added and passed](https://pandas.pydata.org/pandas-docs/dev/development/contributing_codebase.html#writing-tests) if fixing a bug or adding a new feature
- [x] All [code checks passed](https://pandas.pydata.org/pandas-docs/dev/development/contributing_codebase.html#pre-commit).
- [x] Added [type annotations](https://pandas.pydata.org/pandas-docs/dev/development/contributing_codebase.html#type-hints) to new arguments/methods/functions.
- [x] Added an entry in the latest `doc/source/whatsnew/vX.X.X.rst` file if fixing a bug or adding a new feature.
| https://api.github.com/repos/pandas-dev/pandas/pulls/57225 | 2024-02-03T14:57:55Z | 2024-02-03T21:54:36Z | 2024-02-03T21:54:36Z | 2024-02-03T21:55:04Z |
DOC: Replace pandas logo with a conditional image for light and dark … | diff --git a/README.md b/README.md
index 6fa20d237babe..e5329d66c2d89 100644
--- a/README.md
+++ b/README.md
@@ -1,6 +1,7 @@
-<div align="center">
- <img src="https://pandas.pydata.org/static/img/pandas.svg"><br>
-</div>
+<picture align="center">
+ <source media="(prefers-color-scheme: dark)" srcset="https://pandas.pydata.org/static/img/pandas_white.svg">
+ <img alt="Pandas Logo" src="https://pandas.pydata.org/static/img/pandas.svg">
+</picture>
-----------------
| ...themes
Replaced the static pandas logo with a `picture` tag that displays a different logo based on the color scheme. In light mode, the original logo is shown, while in dark mode, a white version of the logo is displayed for better visibility.
In GitHub Dark Theme

In GitHub Light Theme

| https://api.github.com/repos/pandas-dev/pandas/pulls/57223 | 2024-02-03T12:29:31Z | 2024-02-04T18:11:18Z | 2024-02-04T18:11:18Z | 2024-02-04T18:11:28Z |
ENH: Add all warnings check to the assert_produces_warnings, and separate messages for each warning. | diff --git a/pandas/_testing/_warnings.py b/pandas/_testing/_warnings.py
index d9516077788c8..cd2e2b4141ffd 100644
--- a/pandas/_testing/_warnings.py
+++ b/pandas/_testing/_warnings.py
@@ -11,6 +11,7 @@
from typing import (
TYPE_CHECKING,
Literal,
+ Union,
cast,
)
import warnings
@@ -32,7 +33,8 @@ def assert_produces_warning(
] = "always",
check_stacklevel: bool = True,
raise_on_extra_warnings: bool = True,
- match: str | None = None,
+ match: str | tuple[str | None, ...] | None = None,
+ must_find_all_warnings: bool = True,
) -> Generator[list[warnings.WarningMessage], None, None]:
"""
Context manager for running code expected to either raise a specific warning,
@@ -68,8 +70,15 @@ class for all warnings. To raise multiple types of exceptions,
raise_on_extra_warnings : bool, default True
Whether extra warnings not of the type `expected_warning` should
cause the test to fail.
- match : str, optional
- Match warning message.
+ match : {str, tuple[str, ...]}, optional
+ Match warning message. If it's a tuple, it has to be the size of
+ `expected_warning`. If additionally `must_find_all_warnings` is
+ True, each expected warning's message gets matched with a respective
+ match. Otherwise, multiple values get treated as an alternative.
+ must_find_all_warnings : bool, default True
+ If True and `expected_warning` is a tuple, each expected warning
+ type must get encountered. Otherwise, even one expected warning
+ results in success.
Examples
--------
@@ -97,13 +106,35 @@ class for all warnings. To raise multiple types of exceptions,
yield w
finally:
if expected_warning:
- expected_warning = cast(type[Warning], expected_warning)
- _assert_caught_expected_warning(
- caught_warnings=w,
- expected_warning=expected_warning,
- match=match,
- check_stacklevel=check_stacklevel,
- )
+ if isinstance(expected_warning, tuple) and must_find_all_warnings:
+ match = (
+ match
+ if isinstance(match, tuple)
+ else (match,) * len(expected_warning)
+ )
+ for warning_type, warning_match in zip(expected_warning, match):
+ _assert_caught_expected_warnings(
+ caught_warnings=w,
+ expected_warning=warning_type,
+ match=warning_match,
+ check_stacklevel=check_stacklevel,
+ )
+ else:
+ expected_warning = cast(
+ Union[type[Warning], tuple[type[Warning], ...]],
+ expected_warning,
+ )
+ match = (
+ "|".join(m for m in match if m)
+ if isinstance(match, tuple)
+ else match
+ )
+ _assert_caught_expected_warnings(
+ caught_warnings=w,
+ expected_warning=expected_warning,
+ match=match,
+ check_stacklevel=check_stacklevel,
+ )
if raise_on_extra_warnings:
_assert_caught_no_extra_warnings(
caught_warnings=w,
@@ -123,10 +154,10 @@ def maybe_produces_warning(
return nullcontext()
-def _assert_caught_expected_warning(
+def _assert_caught_expected_warnings(
*,
caught_warnings: Sequence[warnings.WarningMessage],
- expected_warning: type[Warning],
+ expected_warning: type[Warning] | tuple[type[Warning], ...],
match: str | None,
check_stacklevel: bool,
) -> None:
@@ -134,6 +165,11 @@ def _assert_caught_expected_warning(
saw_warning = False
matched_message = False
unmatched_messages = []
+ warning_name = (
+ tuple(x.__name__ for x in expected_warning)
+ if isinstance(expected_warning, tuple)
+ else expected_warning.__name__
+ )
for actual_warning in caught_warnings:
if issubclass(actual_warning.category, expected_warning):
@@ -149,13 +185,11 @@ def _assert_caught_expected_warning(
unmatched_messages.append(actual_warning.message)
if not saw_warning:
- raise AssertionError(
- f"Did not see expected warning of class {expected_warning.__name__!r}"
- )
+ raise AssertionError(f"Did not see expected warning of class {warning_name!r}")
if match and not matched_message:
raise AssertionError(
- f"Did not see warning {expected_warning.__name__!r} "
+ f"Did not see warning {warning_name!r} "
f"matching '{match}'. The emitted warning messages are "
f"{unmatched_messages}"
)
diff --git a/pandas/_testing/contexts.py b/pandas/_testing/contexts.py
index b986e03e25815..7ebed8857f0af 100644
--- a/pandas/_testing/contexts.py
+++ b/pandas/_testing/contexts.py
@@ -173,7 +173,7 @@ def raises_chained_assignment_error(warn=True, extra_warnings=(), extra_match=()
elif PYPY and extra_warnings:
return assert_produces_warning(
extra_warnings,
- match="|".join(extra_match),
+ match=extra_match,
)
else:
if using_copy_on_write():
@@ -190,5 +190,5 @@ def raises_chained_assignment_error(warn=True, extra_warnings=(), extra_match=()
warning = (warning, *extra_warnings) # type: ignore[assignment]
return assert_produces_warning(
warning,
- match="|".join((match, *extra_match)),
+ match=(match, *extra_match),
)
diff --git a/pandas/tests/indexing/test_chaining_and_caching.py b/pandas/tests/indexing/test_chaining_and_caching.py
index 2a2772d1b3453..b28c3cba7d310 100644
--- a/pandas/tests/indexing/test_chaining_and_caching.py
+++ b/pandas/tests/indexing/test_chaining_and_caching.py
@@ -284,7 +284,9 @@ def test_detect_chained_assignment_changing_dtype(self):
with tm.raises_chained_assignment_error():
df.loc[2]["C"] = "foo"
tm.assert_frame_equal(df, df_original)
- with tm.raises_chained_assignment_error(extra_warnings=(FutureWarning,)):
+ with tm.raises_chained_assignment_error(
+ extra_warnings=(FutureWarning,), extra_match=(None,)
+ ):
df["C"][2] = "foo"
tm.assert_frame_equal(df, df_original)
diff --git a/pandas/tests/io/parser/common/test_read_errors.py b/pandas/tests/io/parser/common/test_read_errors.py
index 0827f64dccf46..bd47e045417ce 100644
--- a/pandas/tests/io/parser/common/test_read_errors.py
+++ b/pandas/tests/io/parser/common/test_read_errors.py
@@ -196,7 +196,6 @@ def test_warn_bad_lines(all_parsers):
expected_warning = ParserWarning
if parser.engine == "pyarrow":
match_msg = "Expected 1 columns, but found 3: 1,2,3"
- expected_warning = (ParserWarning, DeprecationWarning)
with tm.assert_produces_warning(
expected_warning, match=match_msg, check_stacklevel=False
@@ -315,7 +314,6 @@ def test_on_bad_lines_warn_correct_formatting(all_parsers):
expected_warning = ParserWarning
if parser.engine == "pyarrow":
match_msg = "Expected 2 columns, but found 3: a,b,c"
- expected_warning = (ParserWarning, DeprecationWarning)
with tm.assert_produces_warning(
expected_warning, match=match_msg, check_stacklevel=False
diff --git a/pandas/tests/io/parser/test_parse_dates.py b/pandas/tests/io/parser/test_parse_dates.py
index 0bc0c3e744db7..8968948df5fa9 100644
--- a/pandas/tests/io/parser/test_parse_dates.py
+++ b/pandas/tests/io/parser/test_parse_dates.py
@@ -343,7 +343,7 @@ def test_multiple_date_col(all_parsers, keep_date_col, request):
"names": ["X0", "X1", "X2", "X3", "X4", "X5", "X6", "X7", "X8"],
}
with tm.assert_produces_warning(
- (DeprecationWarning, FutureWarning), match=depr_msg, check_stacklevel=False
+ FutureWarning, match=depr_msg, check_stacklevel=False
):
result = parser.read_csv(StringIO(data), **kwds)
@@ -724,7 +724,7 @@ def test_multiple_date_col_name_collision(all_parsers, data, parse_dates, msg):
)
with pytest.raises(ValueError, match=msg):
with tm.assert_produces_warning(
- (FutureWarning, DeprecationWarning), match=depr_msg, check_stacklevel=False
+ FutureWarning, match=depr_msg, check_stacklevel=False
):
parser.read_csv(StringIO(data), parse_dates=parse_dates)
@@ -1248,14 +1248,14 @@ def test_multiple_date_col_named_index_compat(all_parsers):
"Support for nested sequences for 'parse_dates' in pd.read_csv is deprecated"
)
with tm.assert_produces_warning(
- (FutureWarning, DeprecationWarning), match=depr_msg, check_stacklevel=False
+ FutureWarning, match=depr_msg, check_stacklevel=False
):
with_indices = parser.read_csv(
StringIO(data), parse_dates={"nominal": [1, 2]}, index_col="nominal"
)
with tm.assert_produces_warning(
- (FutureWarning, DeprecationWarning), match=depr_msg, check_stacklevel=False
+ FutureWarning, match=depr_msg, check_stacklevel=False
):
with_names = parser.read_csv(
StringIO(data),
@@ -1280,13 +1280,13 @@ def test_multiple_date_col_multiple_index_compat(all_parsers):
"Support for nested sequences for 'parse_dates' in pd.read_csv is deprecated"
)
with tm.assert_produces_warning(
- (FutureWarning, DeprecationWarning), match=depr_msg, check_stacklevel=False
+ FutureWarning, match=depr_msg, check_stacklevel=False
):
result = parser.read_csv(
StringIO(data), index_col=["nominal", "ID"], parse_dates={"nominal": [1, 2]}
)
with tm.assert_produces_warning(
- (FutureWarning, DeprecationWarning), match=depr_msg, check_stacklevel=False
+ FutureWarning, match=depr_msg, check_stacklevel=False
):
expected = parser.read_csv(StringIO(data), parse_dates={"nominal": [1, 2]})
@@ -2267,7 +2267,7 @@ def test_parse_dates_dict_format_two_columns(all_parsers, key, parse_dates):
"Support for nested sequences for 'parse_dates' in pd.read_csv is deprecated"
)
with tm.assert_produces_warning(
- (FutureWarning, DeprecationWarning), match=depr_msg, check_stacklevel=False
+ FutureWarning, match=depr_msg, check_stacklevel=False
):
result = parser.read_csv(
StringIO(data), date_format={key: "%d- %m-%Y"}, parse_dates=parse_dates
diff --git a/pandas/tests/io/parser/usecols/test_parse_dates.py b/pandas/tests/io/parser/usecols/test_parse_dates.py
index 75efe87c408c0..ab98857e0c178 100644
--- a/pandas/tests/io/parser/usecols/test_parse_dates.py
+++ b/pandas/tests/io/parser/usecols/test_parse_dates.py
@@ -146,7 +146,7 @@ def test_usecols_with_parse_dates4(all_parsers):
"Support for nested sequences for 'parse_dates' in pd.read_csv is deprecated"
)
with tm.assert_produces_warning(
- (FutureWarning, DeprecationWarning), match=depr_msg, check_stacklevel=False
+ FutureWarning, match=depr_msg, check_stacklevel=False
):
result = parser.read_csv(
StringIO(data),
@@ -187,7 +187,7 @@ def test_usecols_with_parse_dates_and_names(all_parsers, usecols, names, request
"Support for nested sequences for 'parse_dates' in pd.read_csv is deprecated"
)
with tm.assert_produces_warning(
- (FutureWarning, DeprecationWarning), match=depr_msg, check_stacklevel=False
+ FutureWarning, match=depr_msg, check_stacklevel=False
):
result = parser.read_csv(
StringIO(s), names=names, parse_dates=parse_dates, usecols=usecols
diff --git a/pandas/tests/util/test_assert_produces_warning.py b/pandas/tests/util/test_assert_produces_warning.py
index 80e3264690f81..5b917dbbe7ba7 100644
--- a/pandas/tests/util/test_assert_produces_warning.py
+++ b/pandas/tests/util/test_assert_produces_warning.py
@@ -42,7 +42,6 @@ def f():
warnings.warn("f2", RuntimeWarning)
-@pytest.mark.filterwarnings("ignore:f1:FutureWarning")
def test_assert_produces_warning_honors_filter():
# Raise by default.
msg = r"Caused unexpected warning\(s\)"
@@ -180,6 +179,44 @@ def test_match_multiple_warnings():
warnings.warn("Match this too", UserWarning)
+def test_must_match_multiple_warnings():
+ # https://github.com/pandas-dev/pandas/issues/56555
+ category = (FutureWarning, UserWarning)
+ msg = "Did not see expected warning of class 'UserWarning'"
+ with pytest.raises(AssertionError, match=msg):
+ with tm.assert_produces_warning(category, match=r"^Match this"):
+ warnings.warn("Match this", FutureWarning)
+
+
+def test_must_match_multiple_warnings_messages():
+ # https://github.com/pandas-dev/pandas/issues/56555
+ category = (FutureWarning, UserWarning)
+ msg = r"The emitted warning messages are \[UserWarning\('Not this'\)\]"
+ with pytest.raises(AssertionError, match=msg):
+ with tm.assert_produces_warning(category, match=r"^Match this"):
+ warnings.warn("Match this", FutureWarning)
+ warnings.warn("Not this", UserWarning)
+
+
+def test_allow_partial_match_for_multiple_warnings():
+ # https://github.com/pandas-dev/pandas/issues/56555
+ category = (FutureWarning, UserWarning)
+ with tm.assert_produces_warning(
+ category, match=r"^Match this", must_find_all_warnings=False
+ ):
+ warnings.warn("Match this", FutureWarning)
+
+
+def test_allow_partial_match_for_multiple_warnings_messages():
+ # https://github.com/pandas-dev/pandas/issues/56555
+ category = (FutureWarning, UserWarning)
+ with tm.assert_produces_warning(
+ category, match=r"^Match this", must_find_all_warnings=False
+ ):
+ warnings.warn("Match this", FutureWarning)
+ warnings.warn("Not this", UserWarning)
+
+
def test_right_category_wrong_match_raises(pair_different_warnings):
target_category, other_category = pair_different_warnings
with pytest.raises(AssertionError, match="Did not see warning.*matching"):
| The function in question gets used in multiple places, that's why I felt it would be safer to preserve current behavior and API, and add an additional, optional parameter, which alters the behavior, enabling checking if all passed warning types get detected.
- [x] closes #56555 (Replace xxxx with the GitHub issue number)
- [x] [Tests added and passed](https://pandas.pydata.org/pandas-docs/dev/development/contributing_codebase.html#writing-tests) if fixing a bug or adding a new feature
- [x] All [code checks passed](https://pandas.pydata.org/pandas-docs/dev/development/contributing_codebase.html#pre-commit).
- [x] Added [type annotations](https://pandas.pydata.org/pandas-docs/dev/development/contributing_codebase.html#type-hints) to new arguments/methods/functions.
- [x] Added an entry in the latest `doc/source/whatsnew/vX.X.X.rst` file if fixing a bug or adding a new feature.
| https://api.github.com/repos/pandas-dev/pandas/pulls/57222 | 2024-02-03T05:30:15Z | 2024-04-14T19:18:21Z | 2024-04-14T19:18:21Z | 2024-04-14T19:18:34Z |
TST: Reduce parameterization of test_str_find_e2e | diff --git a/pandas/tests/extension/test_arrow.py b/pandas/tests/extension/test_arrow.py
index 3ce2b38bf8644..b49f107859159 100644
--- a/pandas/tests/extension/test_arrow.py
+++ b/pandas/tests/extension/test_arrow.py
@@ -23,7 +23,6 @@
BytesIO,
StringIO,
)
-from itertools import combinations
import operator
import pickle
import re
@@ -1993,17 +1992,9 @@ def test_str_find_large_start():
@pytest.mark.skipif(
pa_version_under13p0, reason="https://github.com/apache/arrow/issues/36311"
)
-@pytest.mark.parametrize("start", list(range(-15, 15)) + [None])
-@pytest.mark.parametrize("end", list(range(-15, 15)) + [None])
-@pytest.mark.parametrize(
- "sub",
- ["abcaadef"[x:y] for x, y in combinations(range(len("abcaadef") + 1), r=2)]
- + [
- "",
- "az",
- "abce",
- ],
-)
+@pytest.mark.parametrize("start", [-15, -3, 0, 1, 15, None])
+@pytest.mark.parametrize("end", [-15, -1, 0, 3, 15, None])
+@pytest.mark.parametrize("sub", ["", "az", "abce", "a", "caa"])
def test_str_find_e2e(start, end, sub):
s = pd.Series(
["abcaadef", "abc", "abcdeddefgj8292", "ab", "a", ""],
| This recently added test creates 25k tests and looks to add an extra 6-7 minutes in the CI. Reducing the coverage here to speed the test up | https://api.github.com/repos/pandas-dev/pandas/pulls/57221 | 2024-02-03T05:09:15Z | 2024-02-03T18:00:15Z | 2024-02-03T18:00:15Z | 2024-02-03T18:00:18Z |
CI: Fix using "python" instead of "sys.executable" | diff --git a/scripts/validate_docstrings.py b/scripts/validate_docstrings.py
index 682d64244bc1f..d54592252206e 100755
--- a/scripts/validate_docstrings.py
+++ b/scripts/validate_docstrings.py
@@ -188,7 +188,7 @@ def validate_pep8(self):
file.write(content)
file.flush()
cmd = [
- "python",
+ sys.executable,
"-m",
"flake8",
"--format=%(row)d\t%(col)d\t%(code)s\t%(text)s",
| - [x] closes #57219
- [ ] [Tests added and passed](https://pandas.pydata.org/pandas-docs/dev/development/contributing_codebase.html#writing-tests) if fixing a bug or adding a new feature
- [ ] All [code checks passed](https://pandas.pydata.org/pandas-docs/dev/development/contributing_codebase.html#pre-commit).
- [ ] Added [type annotations](https://pandas.pydata.org/pandas-docs/dev/development/contributing_codebase.html#type-hints) to new arguments/methods/functions.
- [ ] Added an entry in the latest `doc/source/whatsnew/vX.X.X.rst` file if fixing a bug or adding a new feature.
| https://api.github.com/repos/pandas-dev/pandas/pulls/57220 | 2024-02-03T03:25:52Z | 2024-02-03T11:30:00Z | 2024-02-03T11:30:00Z | 2024-02-03T22:21:13Z |
BUG: Fixing broken link in getting_started.md | diff --git a/web/pandas/getting_started.md b/web/pandas/getting_started.md
index cb14e52edad2c..61ef26f984567 100644
--- a/web/pandas/getting_started.md
+++ b/web/pandas/getting_started.md
@@ -6,7 +6,7 @@ The next steps provides the easiest and recommended way to set up your
environment to use pandas. Other installation options can be found in
the [advanced installation page]({{ base_url}}docs/getting_started/install.html).
-1. Download [Anaconda](https://www.anaconda.com/distribution/) for your operating system and
+1. Download [Anaconda](https://www.anaconda.com/download/) for your operating system and
the latest Python version, run the installer, and follow the steps. Please note:
- It is not needed (and discouraged) to install Anaconda as root or administrator.
| - [x] closes #57217
- [x] All [code checks passed](https://pandas.pydata.org/pandas-docs/dev/development/contributing_codebase.html#pre-commit)
| https://api.github.com/repos/pandas-dev/pandas/pulls/57218 | 2024-02-03T01:17:04Z | 2024-02-03T01:23:00Z | 2024-02-03T01:23:00Z | 2024-02-03T01:23:06Z |
BUG: ensure_string_array might modify read-only array inplace | diff --git a/doc/source/whatsnew/v3.0.0.rst b/doc/source/whatsnew/v3.0.0.rst
index 806a46c248e15..53a9ac5236040 100644
--- a/doc/source/whatsnew/v3.0.0.rst
+++ b/doc/source/whatsnew/v3.0.0.rst
@@ -160,7 +160,7 @@ Numeric
Conversion
^^^^^^^^^^
--
+- Bug in :meth:`Series.astype` might modify read-only array inplace when casting to a string dtype (:issue:`57212`)
-
Strings
diff --git a/pandas/_libs/lib.pyx b/pandas/_libs/lib.pyx
index 5eb20960f0e3d..50feda8fb188e 100644
--- a/pandas/_libs/lib.pyx
+++ b/pandas/_libs/lib.pyx
@@ -770,6 +770,9 @@ cpdef ndarray[object] ensure_string_array(
result = result.copy()
elif not copy and result is arr:
already_copied = False
+ elif not copy and not result.flags.writeable:
+ # Weird edge case where result is a view
+ already_copied = False
if issubclass(arr.dtype.type, np.str_):
# short-circuit, all elements are str
diff --git a/pandas/tests/copy_view/test_astype.py b/pandas/tests/copy_view/test_astype.py
index 3c1a157dd2c6a..f280e2143fee0 100644
--- a/pandas/tests/copy_view/test_astype.py
+++ b/pandas/tests/copy_view/test_astype.py
@@ -4,6 +4,7 @@
import pytest
from pandas.compat.pyarrow import pa_version_under12p0
+import pandas.util._test_decorators as td
import pandas as pd
from pandas import (
@@ -139,6 +140,17 @@ def test_astype_string_copy_on_pickle_roundrip():
tm.assert_series_equal(base, base_copy)
+@td.skip_if_no("pyarrow")
+def test_astype_string_read_only_on_pickle_roundrip():
+ # https://github.com/pandas-dev/pandas/issues/54654
+ # ensure_string_array may alter read-only array inplace
+ base = Series(np.array([(1, 2), None, 1], dtype="object"))
+ base_copy = pickle.loads(pickle.dumps(base))
+ base_copy._values.flags.writeable = False
+ base_copy.astype("string[pyarrow]", copy=False)
+ tm.assert_series_equal(base, base_copy)
+
+
def test_astype_dict_dtypes(using_copy_on_write):
df = DataFrame(
{"a": [1, 2, 3], "b": [4, 5, 6], "c": Series([1.5, 1.5, 1.5], dtype="float64")}
| We ran into this in Dask, it's quite the edge case... | https://api.github.com/repos/pandas-dev/pandas/pulls/57212 | 2024-02-02T13:51:24Z | 2024-02-04T18:15:37Z | 2024-02-04T18:15:37Z | 2024-02-04T18:16:05Z |
DOC: fix PR02 errors in docstrings - pandas.core.window.rolling.Rolling.quantile, pandas.core.window.expanding.Expanding.quantile | diff --git a/ci/code_checks.sh b/ci/code_checks.sh
index 50baa1cb0b19f..f1a73c2bee5f9 100755
--- a/ci/code_checks.sh
+++ b/ci/code_checks.sh
@@ -178,9 +178,7 @@ if [[ -z "$CHECK" || "$CHECK" == "docstrings" ]]; then
pandas.core.groupby.DataFrameGroupBy.hist\
pandas.core.groupby.DataFrameGroupBy.plot\
pandas.core.groupby.DataFrameGroupBy.corrwith\
- pandas.core.groupby.SeriesGroupBy.plot\
- pandas.core.window.rolling.Rolling.quantile\
- pandas.core.window.expanding.Expanding.quantile # There should be no backslash in the final line, please keep this comment in the last ignored function
+ pandas.core.groupby.SeriesGroupBy.plot # There should be no backslash in the final line, please keep this comment in the last ignored function
RET=$(($RET + $?)) ; echo $MSG "DONE"
fi
diff --git a/pandas/core/window/expanding.py b/pandas/core/window/expanding.py
index 1bf26c482337c..c048c4a506629 100644
--- a/pandas/core/window/expanding.py
+++ b/pandas/core/window/expanding.py
@@ -664,11 +664,11 @@ def kurt(self, numeric_only: bool = False):
create_section_header("Parameters"),
dedent(
"""
- quantile : float
+ q : float
Quantile to compute. 0 <= quantile <= 1.
.. deprecated:: 2.1.0
- This will be renamed to 'q' in a future version.
+ This was renamed from 'quantile' to 'q' in version 2.1.0.
interpolation : {{'linear', 'lower', 'higher', 'midpoint', 'nearest'}}
This optional parameter specifies the interpolation method to use,
when the desired quantile lies between two data points `i` and `j`:
diff --git a/pandas/core/window/rolling.py b/pandas/core/window/rolling.py
index 72a61b2877809..b55432085b928 100644
--- a/pandas/core/window/rolling.py
+++ b/pandas/core/window/rolling.py
@@ -2502,11 +2502,11 @@ def kurt(self, numeric_only: bool = False):
create_section_header("Parameters"),
dedent(
"""
- quantile : float
+ q : float
Quantile to compute. 0 <= quantile <= 1.
.. deprecated:: 2.1.0
- This will be renamed to 'q' in a future version.
+ This was renamed from 'quantile' to 'q' in version 2.1.0.
interpolation : {{'linear', 'lower', 'higher', 'midpoint', 'nearest'}}
This optional parameter specifies the interpolation method to use,
when the desired quantile lies between two data points `i` and `j`:
| All PR02 Errors resolved in the following cases:
1. scripts/validate_docstrings.py --format=actions --errors=PR02 pandas.core.window.rolling.Rolling.quantile
2. scripts/validate_docstrings.py --format=actions --errors=PR02 pandas.core.window.expanding.Expanding.quantile
OUTPUT:
1. scripts/validate_docstrings.py --format=actions --errors=PR02 pandas.core.window.rolling.Rolling.quantile
```
################################################################################
################################## Validation ##################################
################################################################################
5 Errors found for `pandas.core.window.rolling.Rolling.quantile`:
ES01 No extended summary found
SA05 pandas.Series.rolling in `See Also` section does not need `pandas` prefix, use Series.rolling instead.
SA05 pandas.DataFrame.rolling in `See Also` section does not need `pandas` prefix, use DataFrame.rolling instead.
SA05 pandas.Series.quantile in `See Also` section does not need `pandas` prefix, use Series.quantile instead.
SA05 pandas.DataFrame.quantile in `See Also` section does not need `pandas` prefix, use DataFrame.quantile instead.
```
2. scripts/validate_docstrings.py --format=actions --errors=PR02 pandas.core.window.expanding.Expanding.quantile
```
################################################################################
################################## Validation ##################################
################################################################################
5 Errors found for `pandas.core.window.expanding.Expanding.quantile`:
ES01 No extended summary found
SA05 pandas.Series.expanding in `See Also` section does not need `pandas` prefix, use Series.expanding instead.
SA05 pandas.DataFrame.expanding in `See Also` section does not need `pandas` prefix, use DataFrame.expanding instead.
SA05 pandas.Series.quantile in `See Also` section does not need `pandas` prefix, use Series.quantile instead.
SA05 pandas.DataFrame.quantile in `See Also` section does not need `pandas` prefix, use DataFrame.quantile instead.
```
- [x] xref https://github.com/pandas-dev/pandas/issues/57111
- [x] [Tests added and passed](https://pandas.pydata.org/pandas-docs/dev/development/contributing_codebase.html#writing-tests) if fixing a bug or adding a new feature
- [x] All [code checks passed](https://pandas.pydata.org/pandas-docs/dev/development/contributing_codebase.html#pre-commit).
- [ ] Added [type annotations](https://pandas.pydata.org/pandas-docs/dev/development/contributing_codebase.html#type-hints) to new arguments/methods/functions.
- [ ] Added an entry in the latest `doc/source/whatsnew/vX.X.X.rst` file if fixing a bug or adding a new feature.
| https://api.github.com/repos/pandas-dev/pandas/pulls/57211 | 2024-02-02T08:14:40Z | 2024-02-02T11:59:14Z | 2024-02-02T11:59:14Z | 2024-02-02T17:59:49Z |
DOC: fix PR02 errors in docstrings - pandas.core.groupby.SeriesGroupBy.transform and pandas.core.groupby.DataFrameGroupBy.transform | diff --git a/ci/code_checks.sh b/ci/code_checks.sh
index 50baa1cb0b19f..9e1239c72776e 100755
--- a/ci/code_checks.sh
+++ b/ci/code_checks.sh
@@ -169,8 +169,6 @@ if [[ -z "$CHECK" || "$CHECK" == "docstrings" ]]; then
pandas.Interval\
pandas.Grouper\
pandas.core.groupby.SeriesGroupBy.apply\
- pandas.core.groupby.SeriesGroupBy.transform\
- pandas.core.groupby.DataFrameGroupBy.transform\
pandas.core.groupby.DataFrameGroupBy.nth\
pandas.core.groupby.DataFrameGroupBy.rolling\
pandas.core.groupby.SeriesGroupBy.nth\
diff --git a/pandas/core/groupby/groupby.py b/pandas/core/groupby/groupby.py
index c4ae47348a64c..68626534f1e74 100644
--- a/pandas/core/groupby/groupby.py
+++ b/pandas/core/groupby/groupby.py
@@ -486,7 +486,7 @@ class providing the base-class of operations.
Parameters
----------
-f : function, str
+func : function, str
Function to apply to each group. See the Notes section below for requirements.
Accepted inputs are:
| All PR02 Errors resolved in the following cases:
1. scripts/validate_docstrings.py --format=actions --errors=PR02 pandas.core.groupby.SeriesGroupBy.transform
2. scripts/validate_docstrings.py --format=actions --errors=PR02 pandas.core.groupby.DataFrameGroupBy.transform
OUTPUT:
1. scripts/validate_docstrings.py --format=actions --errors=PR02 pandas.core.groupby.SeriesGroupBy.transform
```
################################################################################
################################## Validation ##################################
################################################################################
1 Errors found for `pandas.core.groupby.SeriesGroupBy.transform`:
RT03 Return value has no description
```
2. scripts/validate_docstrings.py --format=actions --errors=PR02 pandas.core.groupby.DataFrameGroupBy.transform
```
################################################################################
################################## Validation ##################################
################################################################################
1 Errors found for `pandas.core.groupby.DataFrameGroupBy.transform`:
RT03 Return value has no description
```
- [x] xref https://github.com/pandas-dev/pandas/issues/57111
- [x] [Tests added and passed](https://pandas.pydata.org/pandas-docs/dev/development/contributing_codebase.html#writing-tests) if fixing a bug or adding a new feature
- [x] All [code checks passed](https://pandas.pydata.org/pandas-docs/dev/development/contributing_codebase.html#pre-commit).
- [ ] Added [type annotations](https://pandas.pydata.org/pandas-docs/dev/development/contributing_codebase.html#type-hints) to new arguments/methods/functions.
- [ ] Added an entry in the latest `doc/source/whatsnew/vX.X.X.rst` file if fixing a bug or adding a new feature.
| https://api.github.com/repos/pandas-dev/pandas/pulls/57210 | 2024-02-02T06:55:29Z | 2024-02-02T11:51:19Z | 2024-02-02T11:51:19Z | 2024-02-02T17:59:26Z |
REF: Remove internals.base | diff --git a/pandas/core/frame.py b/pandas/core/frame.py
index ca488190a8704..207e3e7635cac 100644
--- a/pandas/core/frame.py
+++ b/pandas/core/frame.py
@@ -259,7 +259,7 @@
from pandas.core.groupby.generic import DataFrameGroupBy
from pandas.core.interchange.dataframe_protocol import DataFrame as DataFrameXchg
- from pandas.core.internals import SingleDataManager
+ from pandas.core.internals.managers import SingleBlockManager
from pandas.io.formats.style import Styler
@@ -4530,14 +4530,14 @@ def _ensure_valid_index(self, value) -> None:
self._mgr = self._mgr.reindex_axis(index_copy, axis=1, fill_value=np.nan)
- def _box_col_values(self, values: SingleDataManager, loc: int) -> Series:
+ def _box_col_values(self, values: SingleBlockManager, loc: int) -> Series:
"""
Provide boxed values for a column.
"""
# Lookup in columns so that if e.g. a str datetime was passed
# we attach the Timestamp object as the name.
name = self.columns[loc]
- # We get index=self.index bc values is a SingleDataManager
+ # We get index=self.index bc values is a SingleBlockManager
obj = self._constructor_sliced_from_mgr(values, axes=values.axes)
obj._name = name
return obj.__finalize__(self)
diff --git a/pandas/core/internals/__init__.py b/pandas/core/internals/__init__.py
index da394e783be4b..fb14c5ad82f4f 100644
--- a/pandas/core/internals/__init__.py
+++ b/pandas/core/internals/__init__.py
@@ -1,8 +1,4 @@
from pandas.core.internals.api import make_block # 2023-09-18 pyarrow uses this
-from pandas.core.internals.base import (
- DataManager,
- SingleDataManager,
-)
from pandas.core.internals.concat import concatenate_managers
from pandas.core.internals.managers import (
BlockManager,
@@ -14,9 +10,7 @@
"DatetimeTZBlock", # pylint: disable=undefined-all-variable
"ExtensionBlock", # pylint: disable=undefined-all-variable
"make_block",
- "DataManager",
"BlockManager",
- "SingleDataManager",
"SingleBlockManager",
"concatenate_managers",
]
diff --git a/pandas/core/internals/base.py b/pandas/core/internals/base.py
deleted file mode 100644
index d6d588d5e2492..0000000000000
--- a/pandas/core/internals/base.py
+++ /dev/null
@@ -1,406 +0,0 @@
-"""
-Base class for the internal managers. BlockManager inherits from this class.
-"""
-from __future__ import annotations
-
-from typing import (
- TYPE_CHECKING,
- Any,
- Literal,
- cast,
- final,
-)
-
-import numpy as np
-
-from pandas._config import (
- using_copy_on_write,
- warn_copy_on_write,
-)
-
-from pandas._libs import (
- algos as libalgos,
- lib,
-)
-from pandas.errors import AbstractMethodError
-from pandas.util._validators import validate_bool_kwarg
-
-from pandas.core.dtypes.cast import (
- find_common_type,
- np_can_hold_element,
-)
-from pandas.core.dtypes.dtypes import (
- ExtensionDtype,
- SparseDtype,
-)
-
-from pandas.core.base import PandasObject
-from pandas.core.construction import extract_array
-from pandas.core.indexes.api import (
- Index,
- default_index,
-)
-
-if TYPE_CHECKING:
- from pandas._typing import (
- ArrayLike,
- AxisInt,
- DtypeObj,
- Self,
- Shape,
- )
-
-
-class _AlreadyWarned:
- def __init__(self) -> None:
- # This class is used on the manager level to the block level to
- # ensure that we warn only once. The block method can update the
- # warned_already option without returning a value to keep the
- # interface consistent. This is only a temporary solution for
- # CoW warnings.
- self.warned_already = False
-
-
-class DataManager(PandasObject):
- # TODO share more methods/attributes
-
- axes: list[Index]
-
- @property
- def items(self) -> Index:
- raise AbstractMethodError(self)
-
- @final
- def __len__(self) -> int:
- return len(self.items)
-
- @property
- def ndim(self) -> int:
- return len(self.axes)
-
- @property
- def shape(self) -> Shape:
- return tuple(len(ax) for ax in self.axes)
-
- @final
- def _validate_set_axis(self, axis: AxisInt, new_labels: Index) -> None:
- # Caller is responsible for ensuring we have an Index object.
- old_len = len(self.axes[axis])
- new_len = len(new_labels)
-
- if axis == 1 and len(self.items) == 0:
- # If we are setting the index on a DataFrame with no columns,
- # it is OK to change the length.
- pass
-
- elif new_len != old_len:
- raise ValueError(
- f"Length mismatch: Expected axis has {old_len} elements, new "
- f"values have {new_len} elements"
- )
-
- def reindex_indexer(
- self,
- new_axis,
- indexer,
- axis: AxisInt,
- fill_value=None,
- allow_dups: bool = False,
- copy: bool = True,
- only_slice: bool = False,
- ) -> Self:
- raise AbstractMethodError(self)
-
- @final
- def reindex_axis(
- self,
- new_index: Index,
- axis: AxisInt,
- fill_value=None,
- only_slice: bool = False,
- ) -> Self:
- """
- Conform data manager to new index.
- """
- new_index, indexer = self.axes[axis].reindex(new_index)
-
- return self.reindex_indexer(
- new_index,
- indexer,
- axis=axis,
- fill_value=fill_value,
- copy=False,
- only_slice=only_slice,
- )
-
- def _equal_values(self, other: Self) -> bool:
- """
- To be implemented by the subclasses. Only check the column values
- assuming shape and indexes have already been checked.
- """
- raise AbstractMethodError(self)
-
- @final
- def equals(self, other: object) -> bool:
- """
- Implementation for DataFrame.equals
- """
- if not isinstance(other, type(self)):
- return False
-
- self_axes, other_axes = self.axes, other.axes
- if len(self_axes) != len(other_axes):
- return False
- if not all(ax1.equals(ax2) for ax1, ax2 in zip(self_axes, other_axes)):
- return False
-
- return self._equal_values(other)
-
- def apply(
- self,
- f,
- align_keys: list[str] | None = None,
- **kwargs,
- ) -> Self:
- raise AbstractMethodError(self)
-
- def apply_with_block(
- self,
- f,
- align_keys: list[str] | None = None,
- **kwargs,
- ) -> Self:
- raise AbstractMethodError(self)
-
- @final
- def isna(self, func) -> Self:
- return self.apply("apply", func=func)
-
- @final
- def fillna(self, value, limit: int | None, inplace: bool, downcast) -> Self:
- if limit is not None:
- # Do this validation even if we go through one of the no-op paths
- limit = libalgos.validate_limit(None, limit=limit)
-
- return self.apply(
- "fillna",
- value=value,
- limit=limit,
- inplace=inplace,
- downcast=downcast,
- using_cow=using_copy_on_write(),
- already_warned=_AlreadyWarned(),
- )
-
- @final
- def where(self, other, cond, align: bool) -> Self:
- if align:
- align_keys = ["other", "cond"]
- else:
- align_keys = ["cond"]
- other = extract_array(other, extract_numpy=True)
-
- return self.apply(
- "where",
- align_keys=align_keys,
- other=other,
- cond=cond,
- using_cow=using_copy_on_write(),
- )
-
- @final
- def putmask(self, mask, new, align: bool = True, warn: bool = True) -> Self:
- if align:
- align_keys = ["new", "mask"]
- else:
- align_keys = ["mask"]
- new = extract_array(new, extract_numpy=True)
-
- already_warned = None
- if warn_copy_on_write():
- already_warned = _AlreadyWarned()
- if not warn:
- already_warned.warned_already = True
-
- return self.apply(
- "putmask",
- align_keys=align_keys,
- mask=mask,
- new=new,
- using_cow=using_copy_on_write(),
- already_warned=already_warned,
- )
-
- @final
- def round(self, decimals: int, using_cow: bool = False) -> Self:
- return self.apply(
- "round",
- decimals=decimals,
- using_cow=using_cow,
- )
-
- @final
- def replace(self, to_replace, value, inplace: bool) -> Self:
- inplace = validate_bool_kwarg(inplace, "inplace")
- # NDFrame.replace ensures the not-is_list_likes here
- assert not lib.is_list_like(to_replace)
- assert not lib.is_list_like(value)
- return self.apply(
- "replace",
- to_replace=to_replace,
- value=value,
- inplace=inplace,
- using_cow=using_copy_on_write(),
- already_warned=_AlreadyWarned(),
- )
-
- @final
- def replace_regex(self, **kwargs) -> Self:
- return self.apply(
- "_replace_regex",
- **kwargs,
- using_cow=using_copy_on_write(),
- already_warned=_AlreadyWarned(),
- )
-
- @final
- def replace_list(
- self,
- src_list: list[Any],
- dest_list: list[Any],
- inplace: bool = False,
- regex: bool = False,
- ) -> Self:
- """do a list replace"""
- inplace = validate_bool_kwarg(inplace, "inplace")
-
- bm = self.apply(
- "replace_list",
- src_list=src_list,
- dest_list=dest_list,
- inplace=inplace,
- regex=regex,
- using_cow=using_copy_on_write(),
- already_warned=_AlreadyWarned(),
- )
- bm._consolidate_inplace()
- return bm
-
- def interpolate(self, inplace: bool, **kwargs) -> Self:
- return self.apply(
- "interpolate",
- inplace=inplace,
- **kwargs,
- using_cow=using_copy_on_write(),
- already_warned=_AlreadyWarned(),
- )
-
- def pad_or_backfill(self, inplace: bool, **kwargs) -> Self:
- return self.apply(
- "pad_or_backfill",
- inplace=inplace,
- **kwargs,
- using_cow=using_copy_on_write(),
- already_warned=_AlreadyWarned(),
- )
-
- def shift(self, periods: int, fill_value) -> Self:
- if fill_value is lib.no_default:
- fill_value = None
-
- return self.apply("shift", periods=periods, fill_value=fill_value)
-
- # --------------------------------------------------------------------
- # Consolidation: No-ops for all but BlockManager
-
- def is_consolidated(self) -> bool:
- return True
-
- def consolidate(self) -> Self:
- return self
-
- def _consolidate_inplace(self) -> None:
- return
-
-
-class SingleDataManager(DataManager):
- @property
- def ndim(self) -> Literal[1]:
- return 1
-
- @final
- @property
- def array(self) -> ArrayLike:
- """
- Quick access to the backing array of the Block.
- """
- # error: "SingleDataManager" has no attribute "arrays"; maybe "array"
- return self.arrays[0] # type: ignore[attr-defined]
-
- def setitem_inplace(self, indexer, value, warn: bool = True) -> None:
- """
- Set values with indexer.
-
- For SingleBlockManager, this backs s[indexer] = value
-
- This is an inplace version of `setitem()`, mutating the manager/values
- in place, not returning a new Manager (and Block), and thus never changing
- the dtype.
- """
- arr = self.array
-
- # EAs will do this validation in their own __setitem__ methods.
- if isinstance(arr, np.ndarray):
- # Note: checking for ndarray instead of np.dtype means we exclude
- # dt64/td64, which do their own validation.
- value = np_can_hold_element(arr.dtype, value)
-
- if isinstance(value, np.ndarray) and value.ndim == 1 and len(value) == 1:
- # NumPy 1.25 deprecation: https://github.com/numpy/numpy/pull/10615
- value = value[0, ...]
-
- arr[indexer] = value
-
- def grouped_reduce(self, func):
- arr = self.array
- res = func(arr)
- index = default_index(len(res))
-
- mgr = type(self).from_array(res, index)
- return mgr
-
- @classmethod
- def from_array(cls, arr: ArrayLike, index: Index):
- raise AbstractMethodError(cls)
-
-
-def interleaved_dtype(dtypes: list[DtypeObj]) -> DtypeObj | None:
- """
- Find the common dtype for `blocks`.
-
- Parameters
- ----------
- blocks : List[DtypeObj]
-
- Returns
- -------
- dtype : np.dtype, ExtensionDtype, or None
- None is returned when `blocks` is empty.
- """
- if not len(dtypes):
- return None
-
- return find_common_type(dtypes)
-
-
-def ensure_np_dtype(dtype: DtypeObj) -> np.dtype:
- # TODO: https://github.com/pandas-dev/pandas/issues/22791
- # Give EAs some input on what happens here. Sparse needs this.
- if isinstance(dtype, SparseDtype):
- dtype = dtype.subtype
- dtype = cast(np.dtype, dtype)
- elif isinstance(dtype, ExtensionDtype):
- dtype = np.dtype("object")
- elif dtype == np.dtype(str):
- dtype = np.dtype("object")
- return dtype
diff --git a/pandas/core/internals/managers.py b/pandas/core/internals/managers.py
index c6a8b61e0c51e..e1a18cb79a1d6 100644
--- a/pandas/core/internals/managers.py
+++ b/pandas/core/internals/managers.py
@@ -7,9 +7,11 @@
import itertools
from typing import (
TYPE_CHECKING,
+ Any,
Callable,
Literal,
cast,
+ final,
)
import warnings
import weakref
@@ -22,6 +24,7 @@
)
from pandas._libs import (
+ algos as libalgos,
internals as libinternals,
lib,
)
@@ -30,11 +33,19 @@
BlockValuesRefs,
)
from pandas._libs.tslibs import Timestamp
-from pandas.errors import PerformanceWarning
+from pandas.errors import (
+ AbstractMethodError,
+ PerformanceWarning,
+)
from pandas.util._decorators import cache_readonly
from pandas.util._exceptions import find_stack_level
+from pandas.util._validators import validate_bool_kwarg
-from pandas.core.dtypes.cast import infer_dtype_from_scalar
+from pandas.core.dtypes.cast import (
+ find_common_type,
+ infer_dtype_from_scalar,
+ np_can_hold_element,
+)
from pandas.core.dtypes.common import (
ensure_platform_int,
is_1d_only_ea_dtype,
@@ -43,6 +54,7 @@
from pandas.core.dtypes.dtypes import (
DatetimeTZDtype,
ExtensionDtype,
+ SparseDtype,
)
from pandas.core.dtypes.generic import (
ABCDataFrame,
@@ -60,6 +72,7 @@
DatetimeArray,
)
from pandas.core.arrays._mixins import NDArrayBackedExtensionArray
+from pandas.core.base import PandasObject
from pandas.core.construction import (
ensure_wrapped_if_datetimelike,
extract_array,
@@ -67,14 +80,9 @@
from pandas.core.indexers import maybe_convert_indices
from pandas.core.indexes.api import (
Index,
+ default_index,
ensure_index,
)
-from pandas.core.internals.base import (
- DataManager,
- SingleDataManager,
- ensure_np_dtype,
- interleaved_dtype,
-)
from pandas.core.internals.blocks import (
COW_WARNING_GENERAL_MSG,
COW_WARNING_SETITEM_MSG,
@@ -106,7 +114,49 @@
from pandas.api.extensions import ExtensionArray
-class BaseBlockManager(DataManager):
+def interleaved_dtype(dtypes: list[DtypeObj]) -> DtypeObj | None:
+ """
+ Find the common dtype for `blocks`.
+
+ Parameters
+ ----------
+ blocks : List[DtypeObj]
+
+ Returns
+ -------
+ dtype : np.dtype, ExtensionDtype, or None
+ None is returned when `blocks` is empty.
+ """
+ if not len(dtypes):
+ return None
+
+ return find_common_type(dtypes)
+
+
+def ensure_np_dtype(dtype: DtypeObj) -> np.dtype:
+ # TODO: https://github.com/pandas-dev/pandas/issues/22791
+ # Give EAs some input on what happens here. Sparse needs this.
+ if isinstance(dtype, SparseDtype):
+ dtype = dtype.subtype
+ dtype = cast(np.dtype, dtype)
+ elif isinstance(dtype, ExtensionDtype):
+ dtype = np.dtype("object")
+ elif dtype == np.dtype(str):
+ dtype = np.dtype("object")
+ return dtype
+
+
+class _AlreadyWarned:
+ def __init__(self) -> None:
+ # This class is used on the manager level to the block level to
+ # ensure that we warn only once. The block method can update the
+ # warned_already option without returning a value to keep the
+ # interface consistent. This is only a temporary solution for
+ # CoW warnings.
+ self.warned_already = False
+
+
+class BaseBlockManager(PandasObject):
"""
Core internal data structure to implement DataFrame, Series, etc.
@@ -174,6 +224,14 @@ def ndim(self) -> int:
def __init__(self, blocks, axes, verify_integrity: bool = True) -> None:
raise NotImplementedError
+ @final
+ def __len__(self) -> int:
+ return len(self.items)
+
+ @property
+ def shape(self) -> Shape:
+ return tuple(len(ax) for ax in self.axes)
+
@classmethod
def from_blocks(cls, blocks: list[Block], axes: list[Index]) -> Self:
raise NotImplementedError
@@ -239,6 +297,23 @@ def set_axis(self, axis: AxisInt, new_labels: Index) -> None:
self._validate_set_axis(axis, new_labels)
self.axes[axis] = new_labels
+ @final
+ def _validate_set_axis(self, axis: AxisInt, new_labels: Index) -> None:
+ # Caller is responsible for ensuring we have an Index object.
+ old_len = len(self.axes[axis])
+ new_len = len(new_labels)
+
+ if axis == 1 and len(self.items) == 0:
+ # If we are setting the index on a DataFrame with no columns,
+ # it is OK to change the length.
+ pass
+
+ elif new_len != old_len:
+ raise ValueError(
+ f"Length mismatch: Expected axis has {old_len} elements, new "
+ f"values have {new_len} elements"
+ )
+
@property
def is_single_block(self) -> bool:
# Assumes we are 2D; overridden by SingleBlockManager
@@ -315,6 +390,29 @@ def __repr__(self) -> str:
output += f"\n{block}"
return output
+ def _equal_values(self, other: Self) -> bool:
+ """
+ To be implemented by the subclasses. Only check the column values
+ assuming shape and indexes have already been checked.
+ """
+ raise AbstractMethodError(self)
+
+ @final
+ def equals(self, other: object) -> bool:
+ """
+ Implementation for DataFrame.equals
+ """
+ if not isinstance(other, type(self)):
+ return False
+
+ self_axes, other_axes = self.axes, other.axes
+ if len(self_axes) != len(other_axes):
+ return False
+ if not all(ax1.equals(ax2) for ax1, ax2 in zip(self_axes, other_axes)):
+ return False
+
+ return self._equal_values(other)
+
def apply(
self,
f,
@@ -367,6 +465,152 @@ def apply(
out = type(self).from_blocks(result_blocks, self.axes)
return out
+ def apply_with_block(
+ self,
+ f,
+ align_keys: list[str] | None = None,
+ **kwargs,
+ ) -> Self:
+ raise AbstractMethodError(self)
+
+ @final
+ def isna(self, func) -> Self:
+ return self.apply("apply", func=func)
+
+ @final
+ def fillna(self, value, limit: int | None, inplace: bool, downcast) -> Self:
+ if limit is not None:
+ # Do this validation even if we go through one of the no-op paths
+ limit = libalgos.validate_limit(None, limit=limit)
+
+ return self.apply(
+ "fillna",
+ value=value,
+ limit=limit,
+ inplace=inplace,
+ downcast=downcast,
+ using_cow=using_copy_on_write(),
+ already_warned=_AlreadyWarned(),
+ )
+
+ @final
+ def where(self, other, cond, align: bool) -> Self:
+ if align:
+ align_keys = ["other", "cond"]
+ else:
+ align_keys = ["cond"]
+ other = extract_array(other, extract_numpy=True)
+
+ return self.apply(
+ "where",
+ align_keys=align_keys,
+ other=other,
+ cond=cond,
+ using_cow=using_copy_on_write(),
+ )
+
+ @final
+ def putmask(self, mask, new, align: bool = True, warn: bool = True) -> Self:
+ if align:
+ align_keys = ["new", "mask"]
+ else:
+ align_keys = ["mask"]
+ new = extract_array(new, extract_numpy=True)
+
+ already_warned = None
+ if warn_copy_on_write():
+ already_warned = _AlreadyWarned()
+ if not warn:
+ already_warned.warned_already = True
+
+ return self.apply(
+ "putmask",
+ align_keys=align_keys,
+ mask=mask,
+ new=new,
+ using_cow=using_copy_on_write(),
+ already_warned=already_warned,
+ )
+
+ @final
+ def round(self, decimals: int, using_cow: bool = False) -> Self:
+ return self.apply(
+ "round",
+ decimals=decimals,
+ using_cow=using_cow,
+ )
+
+ @final
+ def replace(self, to_replace, value, inplace: bool) -> Self:
+ inplace = validate_bool_kwarg(inplace, "inplace")
+ # NDFrame.replace ensures the not-is_list_likes here
+ assert not lib.is_list_like(to_replace)
+ assert not lib.is_list_like(value)
+ return self.apply(
+ "replace",
+ to_replace=to_replace,
+ value=value,
+ inplace=inplace,
+ using_cow=using_copy_on_write(),
+ already_warned=_AlreadyWarned(),
+ )
+
+ @final
+ def replace_regex(self, **kwargs) -> Self:
+ return self.apply(
+ "_replace_regex",
+ **kwargs,
+ using_cow=using_copy_on_write(),
+ already_warned=_AlreadyWarned(),
+ )
+
+ @final
+ def replace_list(
+ self,
+ src_list: list[Any],
+ dest_list: list[Any],
+ inplace: bool = False,
+ regex: bool = False,
+ ) -> Self:
+ """do a list replace"""
+ inplace = validate_bool_kwarg(inplace, "inplace")
+
+ bm = self.apply(
+ "replace_list",
+ src_list=src_list,
+ dest_list=dest_list,
+ inplace=inplace,
+ regex=regex,
+ using_cow=using_copy_on_write(),
+ already_warned=_AlreadyWarned(),
+ )
+ bm._consolidate_inplace()
+ return bm
+
+ def interpolate(self, inplace: bool, **kwargs) -> Self:
+ return self.apply(
+ "interpolate",
+ inplace=inplace,
+ **kwargs,
+ using_cow=using_copy_on_write(),
+ already_warned=_AlreadyWarned(),
+ )
+
+ def pad_or_backfill(self, inplace: bool, **kwargs) -> Self:
+ return self.apply(
+ "pad_or_backfill",
+ inplace=inplace,
+ **kwargs,
+ using_cow=using_copy_on_write(),
+ already_warned=_AlreadyWarned(),
+ )
+
+ def shift(self, periods: int, fill_value) -> Self:
+ if fill_value is lib.no_default:
+ fill_value = None
+
+ return self.apply("shift", periods=periods, fill_value=fill_value)
+
def setitem(self, indexer, value, warn: bool = True) -> Self:
"""
Set values with indexer.
@@ -602,6 +846,9 @@ def copy_func(ax):
res._consolidate_inplace()
return res
+ def is_consolidated(self) -> bool:
+ return True
+
def consolidate(self) -> Self:
"""
Join together blocks having same dtype
@@ -618,6 +865,31 @@ def consolidate(self) -> Self:
bm._consolidate_inplace()
return bm
+ def _consolidate_inplace(self) -> None:
+ return
+
+ @final
+ def reindex_axis(
+ self,
+ new_index: Index,
+ axis: AxisInt,
+ fill_value=None,
+ only_slice: bool = False,
+ ) -> Self:
+ """
+ Conform data manager to new index.
+ """
+ new_index, indexer = self.axes[axis].reindex(new_index)
+
+ return self.reindex_indexer(
+ new_index,
+ indexer,
+ axis=axis,
+ fill_value=fill_value,
+ copy=False,
+ only_slice=only_slice,
+ )
+
def reindex_indexer(
self,
new_axis: Index,
@@ -1820,7 +2092,7 @@ def concat_vertical(cls, mgrs: list[Self], axes: list[Index]) -> Self:
raise NotImplementedError("This logic lives (for now) in internals.concat")
-class SingleBlockManager(BaseBlockManager, SingleDataManager):
+class SingleBlockManager(BaseBlockManager):
"""manage a single block with"""
@property
@@ -1939,6 +2211,14 @@ def _post_setstate(self) -> None:
def _block(self) -> Block:
return self.blocks[0]
+ @final
+ @property
+ def array(self) -> ArrayLike:
+ """
+ Quick access to the backing array of the Block.
+ """
+ return self.arrays[0]
+
# error: Cannot override writeable attribute with read-only property
@property
def _blknos(self) -> None: # type: ignore[override]
@@ -2041,7 +2321,19 @@ def setitem_inplace(self, indexer, value, warn: bool = True) -> None:
stacklevel=find_stack_level(),
)
- super().setitem_inplace(indexer, value)
+ arr = self.array
+
+ # EAs will do this validation in their own __setitem__ methods.
+ if isinstance(arr, np.ndarray):
+ # Note: checking for ndarray instead of np.dtype means we exclude
+ # dt64/td64, which do their own validation.
+ value = np_can_hold_element(arr.dtype, value)
+
+ if isinstance(value, np.ndarray) and value.ndim == 1 and len(value) == 1:
+ # NumPy 1.25 deprecation: https://github.com/numpy/numpy/pull/10615
+ value = value[0, ...]
+
+ arr[indexer] = value
def idelete(self, indexer) -> SingleBlockManager:
"""
@@ -2087,6 +2379,14 @@ def _equal_values(self, other: Self) -> bool:
right = other.blocks[0].values
return array_equals(left, right)
+ def grouped_reduce(self, func):
+ arr = self.array
+ res = func(arr)
+ index = default_index(len(res))
+
+ mgr = type(self).from_array(res, index)
+ return mgr
+
# --------------------------------------------------------------------
# Constructor Helpers
diff --git a/pandas/tests/internals/test_api.py b/pandas/tests/internals/test_api.py
index ccd7222fb16e1..7c1d3ff774d0a 100644
--- a/pandas/tests/internals/test_api.py
+++ b/pandas/tests/internals/test_api.py
@@ -23,15 +23,12 @@ def test_namespace():
"concat",
"managers",
"construction",
- "base",
"api",
"ops",
]
expected = [
"make_block",
- "DataManager",
"BlockManager",
- "SingleDataManager",
"SingleBlockManager",
"concatenate_managers",
]
| - [x] closes #57187 (Replace xxxx with the GitHub issue number)
- [ ] [Tests added and passed](https://pandas.pydata.org/pandas-docs/dev/development/contributing_codebase.html#writing-tests) if fixing a bug or adding a new feature
- [ ] All [code checks passed](https://pandas.pydata.org/pandas-docs/dev/development/contributing_codebase.html#pre-commit).
- [ ] Added [type annotations](https://pandas.pydata.org/pandas-docs/dev/development/contributing_codebase.html#type-hints) to new arguments/methods/functions.
- [ ] Added an entry in the latest `doc/source/whatsnew/vX.X.X.rst` file if fixing a bug or adding a new feature.
| https://api.github.com/repos/pandas-dev/pandas/pulls/57208 | 2024-02-02T04:16:15Z | 2024-02-02T17:53:30Z | 2024-02-02T17:53:30Z | 2024-02-02T18:15:12Z |
DEPR: Enforce deprecation of groupby(...).grouper | diff --git a/doc/source/whatsnew/v3.0.0.rst b/doc/source/whatsnew/v3.0.0.rst
index f9117253b61c1..50744ed7a346c 100644
--- a/doc/source/whatsnew/v3.0.0.rst
+++ b/doc/source/whatsnew/v3.0.0.rst
@@ -102,6 +102,7 @@ Deprecations
Removal of prior version deprecations/changes
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- 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`)
diff --git a/pandas/core/groupby/groupby.py b/pandas/core/groupby/groupby.py
index c4ae47348a64c..e21a868df5a54 100644
--- a/pandas/core/groupby/groupby.py
+++ b/pandas/core/groupby/groupby.py
@@ -794,17 +794,6 @@ def __repr__(self) -> str:
# TODO: Better repr for GroupBy object
return object.__repr__(self)
- @final
- @property
- def grouper(self) -> ops.BaseGrouper:
- warnings.warn(
- f"{type(self).__name__}.grouper is deprecated and will be removed in a "
- "future version of pandas.",
- category=FutureWarning,
- stacklevel=find_stack_level(),
- )
- return self._grouper
-
@final
@property
def groups(self) -> dict[Hashable, np.ndarray]:
diff --git a/pandas/core/groupby/grouper.py b/pandas/core/groupby/grouper.py
index 4b9cf5ab75525..f377c9d03d05a 100644
--- a/pandas/core/groupby/grouper.py
+++ b/pandas/core/groupby/grouper.py
@@ -428,17 +428,6 @@ def obj(self):
)
return self._obj_deprecated
- @final
- @property
- def grouper(self):
- warnings.warn(
- f"{type(self).__name__}.grouper is deprecated and will be removed "
- "in a future version. Use GroupBy.grouper instead.",
- FutureWarning,
- stacklevel=find_stack_level(),
- )
- return self._grouper_deprecated
-
@final
@property
def groups(self):
diff --git a/pandas/tests/groupby/test_grouping.py b/pandas/tests/groupby/test_grouping.py
index 841dd29edab10..ee1df1242442f 100644
--- a/pandas/tests/groupby/test_grouping.py
+++ b/pandas/tests/groupby/test_grouping.py
@@ -414,9 +414,7 @@ def test_grouper_getting_correct_binner(self):
def test_grouper_iter(self, df):
gb = df.groupby("A")
- msg = "DataFrameGroupBy.grouper is deprecated"
- with tm.assert_produces_warning(FutureWarning, match=msg):
- grouper = gb.grouper
+ grouper = gb._grouper
result = sorted(grouper)
expected = ["bar", "foo"]
assert result == expected
@@ -428,9 +426,7 @@ def test_empty_groups(self, df):
def test_groupby_grouper(self, df):
grouped = df.groupby("A")
- msg = "DataFrameGroupBy.grouper is deprecated"
- with tm.assert_produces_warning(FutureWarning, match=msg):
- grouper = grouped.grouper
+ grouper = grouped._grouper
result = df.groupby(grouper).mean(numeric_only=True)
expected = grouped.mean(numeric_only=True)
tm.assert_frame_equal(result, expected)
@@ -791,9 +787,7 @@ def test_groupby_empty(self):
# check name
gb = s.groupby(s)
- msg = "SeriesGroupBy.grouper is deprecated"
- with tm.assert_produces_warning(FutureWarning, match=msg):
- grouper = gb.grouper
+ grouper = gb._grouper
result = grouper.names
expected = ["name"]
assert result == expected
@@ -1155,11 +1149,6 @@ def test_grouper_groups():
res = grper.groups
assert res is gb.groups
- msg = "Use GroupBy.grouper instead"
- with tm.assert_produces_warning(FutureWarning, match=msg):
- res = grper.grouper
- assert res is gb._grouper
-
msg = "Grouper.obj is deprecated and will be removed"
with tm.assert_produces_warning(FutureWarning, match=msg):
res = grper.obj
| - [ ] closes #xxxx (Replace xxxx with the GitHub issue number)
- [ ] [Tests added and passed](https://pandas.pydata.org/pandas-docs/dev/development/contributing_codebase.html#writing-tests) if fixing a bug or adding a new feature
- [ ] All [code checks passed](https://pandas.pydata.org/pandas-docs/dev/development/contributing_codebase.html#pre-commit).
- [ ] Added [type annotations](https://pandas.pydata.org/pandas-docs/dev/development/contributing_codebase.html#type-hints) to new arguments/methods/functions.
- [ ] Added an entry in the latest `doc/source/whatsnew/vX.X.X.rst` file if fixing a bug or adding a new feature.
Enforces #56521 | https://api.github.com/repos/pandas-dev/pandas/pulls/57207 | 2024-02-02T03:32:27Z | 2024-02-02T22:54:29Z | 2024-02-02T22:54:29Z | 2024-02-02T23:00:53Z |
DOC: Fix minor typo in indexing.rst | diff --git a/doc/source/user_guide/indexing.rst b/doc/source/user_guide/indexing.rst
index 4954ee1538697..7c8d3b9e1c869 100644
--- a/doc/source/user_guide/indexing.rst
+++ b/doc/source/user_guide/indexing.rst
@@ -1730,7 +1730,7 @@ Returning a view versus a copy
.. warning::
:ref:`Copy-on-Write <copy_on_write>`
- will become the new default in pandas 3.0. This means than chained indexing will
+ will become the new default in pandas 3.0. This means that chained indexing will
never work. As a consequence, the ``SettingWithCopyWarning`` won't be necessary
anymore.
See :ref:`this section <copy_on_write_chained_assignment>`
@@ -1784,7 +1784,7 @@ Why does assignment fail when using chained indexing?
.. warning::
:ref:`Copy-on-Write <copy_on_write>`
- will become the new default in pandas 3.0. This means than chained indexing will
+ will become the new default in pandas 3.0. This means that chained indexing will
never work. As a consequence, the ``SettingWithCopyWarning`` won't be necessary
anymore.
See :ref:`this section <copy_on_write_chained_assignment>`
| - [ ] closes #xxxx (Replace xxxx with the GitHub issue number)
- [ ] [Tests added and passed](https://pandas.pydata.org/pandas-docs/dev/development/contributing_codebase.html#writing-tests) if fixing a bug or adding a new feature
- [ ] All [code checks passed](https://pandas.pydata.org/pandas-docs/dev/development/contributing_codebase.html#pre-commit).
- [ ] Added [type annotations](https://pandas.pydata.org/pandas-docs/dev/development/contributing_codebase.html#type-hints) to new arguments/methods/functions.
- [ ] Added an entry in the latest `doc/source/whatsnew/vX.X.X.rst` file if fixing a bug or adding a new feature.
| https://api.github.com/repos/pandas-dev/pandas/pulls/57206 | 2024-02-02T03:24:56Z | 2024-02-02T12:00:41Z | 2024-02-02T12:00:41Z | 2024-02-04T03:49:02Z |
PERF: Avoid Series constructor in DataFrame(dict(...), columns=) | diff --git a/doc/source/whatsnew/v3.0.0.rst b/doc/source/whatsnew/v3.0.0.rst
index 3fdb4af52a8c0..ca50fd2037b69 100644
--- a/doc/source/whatsnew/v3.0.0.rst
+++ b/doc/source/whatsnew/v3.0.0.rst
@@ -166,6 +166,7 @@ Removal of prior version deprecations/changes
Performance improvements
~~~~~~~~~~~~~~~~~~~~~~~~
+- Performance improvement in :class:`DataFrame` when ``data`` is a ``dict`` and ``columns`` is specified (:issue:`24368`)
- Performance improvement in :meth:`DataFrame.join` for sorted but non-unique indexes (:issue:`56941`)
- Performance improvement in :meth:`DataFrame.join` when left and/or right are non-unique and ``how`` is ``"left"``, ``"right"``, or ``"inner"`` (:issue:`56817`)
- Performance improvement in :meth:`DataFrame.join` with ``how="left"`` or ``how="right"`` and ``sort=True`` (:issue:`56919`)
@@ -288,6 +289,7 @@ Styler
Other
^^^^^
+- Bug in :class:`DataFrame` when passing a ``dict`` with a NA scalar and ``columns`` that would always return ``np.nan`` (:issue:`57205`)
- Bug in :func:`tseries.api.guess_datetime_format` would fail to infer time format when "%Y" == "%H%M" (:issue:`57452`)
- Bug in :meth:`DataFrame.sort_index` when passing ``axis="columns"`` and ``ignore_index=True`` and ``ascending=False`` not returning a :class:`RangeIndex` columns (:issue:`57293`)
- Bug in :meth:`DataFrame.where` where using a non-bool type array in the function would return a ``ValueError`` instead of a ``TypeError`` (:issue:`56330`)
diff --git a/pandas/core/internals/construction.py b/pandas/core/internals/construction.py
index 047c25f4931a6..aab0f1c6dac3c 100644
--- a/pandas/core/internals/construction.py
+++ b/pandas/core/internals/construction.py
@@ -31,12 +31,14 @@
is_list_like,
is_named_tuple,
is_object_dtype,
+ is_scalar,
)
from pandas.core.dtypes.dtypes import ExtensionDtype
from pandas.core.dtypes.generic import (
ABCDataFrame,
ABCSeries,
)
+from pandas.core.dtypes.missing import isna
from pandas.core import (
algorithms,
@@ -354,45 +356,48 @@ def dict_to_mgr(
Used in DataFrame.__init__
"""
- arrays: Sequence[Any] | Series
+ arrays: Sequence[Any]
if columns is not None:
- from pandas.core.series import Series
+ columns = ensure_index(columns)
+ arrays = [np.nan] * len(columns)
+ midxs = set()
+ data_keys = ensure_index(data.keys()) # type: ignore[arg-type]
+ data_values = list(data.values())
+
+ for i, column in enumerate(columns):
+ try:
+ idx = data_keys.get_loc(column)
+ except KeyError:
+ midxs.add(i)
+ continue
+ array = data_values[idx]
+ arrays[i] = array
+ if is_scalar(array) and isna(array):
+ midxs.add(i)
- arrays = Series(data, index=columns, dtype=object)
- missing = arrays.isna()
if index is None:
# GH10856
# raise ValueError if only scalars in dict
- index = _extract_index(arrays[~missing])
+ if midxs:
+ index = _extract_index(
+ [array for i, array in enumerate(arrays) if i not in midxs]
+ )
+ else:
+ index = _extract_index(arrays)
else:
index = ensure_index(index)
# no obvious "empty" int column
- if missing.any() and not is_integer_dtype(dtype):
- nan_dtype: DtypeObj
-
- if dtype is not None:
- # calling sanitize_array ensures we don't mix-and-match
- # NA dtypes
- midxs = missing.values.nonzero()[0]
- for i in midxs:
- arr = sanitize_array(arrays.iat[i], index, dtype=dtype)
- arrays.iat[i] = arr
- else:
- # GH#1783
- nan_dtype = np.dtype("object")
- val = construct_1d_arraylike_from_scalar(np.nan, len(index), nan_dtype)
- nmissing = missing.sum()
- if copy:
- rhs = [val] * nmissing
- else:
- # GH#45369
- rhs = [val.copy() for _ in range(nmissing)]
- arrays.loc[missing] = rhs
-
- arrays = list(arrays)
- columns = ensure_index(columns)
+ if midxs and not is_integer_dtype(dtype):
+ # GH#1783
+ for i in midxs:
+ arr = construct_1d_arraylike_from_scalar(
+ arrays[i],
+ len(index),
+ dtype if dtype is not None else np.dtype("object"),
+ )
+ arrays[i] = arr
else:
keys = list(data.keys())
diff --git a/pandas/tests/frame/test_constructors.py b/pandas/tests/frame/test_constructors.py
index 2bbb20c842dba..7d1a5b4492740 100644
--- a/pandas/tests/frame/test_constructors.py
+++ b/pandas/tests/frame/test_constructors.py
@@ -3042,6 +3042,11 @@ def test_columns_indexes_raise_on_sets(self):
with pytest.raises(ValueError, match="columns cannot be a set"):
DataFrame(data, columns={"a", "b", "c"})
+ def test_from_dict_with_columns_na_scalar(self):
+ result = DataFrame({"a": pd.NaT}, columns=["a"], index=range(2))
+ expected = DataFrame({"a": Series([pd.NaT, pd.NaT])})
+ tm.assert_frame_equal(result, expected)
+
def get1(obj): # TODO: make a helper in tm?
if isinstance(obj, Series):
| - [ ] closes #24368 (Replace xxxx with the GitHub issue number)
- [ ] closed #25887
- [ ] Added an entry in the latest `doc/source/whatsnew/vX.X.X.rst` file if fixing a bug or adding a new feature.
```python
import pandas as pd, numpy as np
a = pd.Series(np.array([1, 2, 3]))
columns = range(10_000)
data = {i: a for i in columns}
%timeit pd.DataFrame(data, columns=columns)
51.3 ms ± 96 µs per loop (mean ± std. dev. of 7 runs, 10 loops each) # PR
102 ms ± 1.8 ms per loop (mean ± std. dev. of 7 runs, 10 loops each) # main
```
```python
In [1]: import pandas as pd
In [2]: %timeit pd.DataFrame([], columns=['a'])
37.3 µs ± 231 ns per loop (mean ± std. dev. of 7 runs, 10,000 loops each) # PR
183 µs ± 584 ns per loop (mean ± std. dev. of 7 runs, 10,000 loops each) # main
``` | https://api.github.com/repos/pandas-dev/pandas/pulls/57205 | 2024-02-02T02:36:52Z | 2024-02-24T00:13:52Z | 2024-02-24T00:13:52Z | 2024-02-24T00:13:55Z |
PDEP-14: Publish translations of pandas.pydata.org | diff --git a/web/pandas/pdeps/0014-translate-website-content.md b/web/pandas/pdeps/0014-translate-website-content.md
new file mode 100644
index 0000000000000..bffc6ae0a8081
--- /dev/null
+++ b/web/pandas/pdeps/0014-translate-website-content.md
@@ -0,0 +1,68 @@
+# PDEP-14: Publish translations of pandas.pydata.org
+
+- Created: 01 February 2024
+- Status: Under discussion
+- Discussion: [#56301](https://github.com/pandas-dev/pandas/issues/56301)
+ [#57204](https://github.com/pandas-dev/pandas/pull/57204)
+- Author: [Albert Steppi](https://github.com/steppi),
+- Revision: 1
+
+## Abstract
+
+The suggestion is to have official translations made for content of the core
+project website [pandas.pydata.org](https://pandas.pydata.org) and offer
+a low friction way for users to access these translations on the core
+project website.
+
+## Motivation, Scope, Usage, and Impact
+
+There are many potential users with no or a low level of English proficiency
+who could benefit from quality official translations of the Pandas website
+content. Though translations for all documentation would be valuable,
+producing and maintaining translations for such a large and oft-changing
+collection of text would take an immense and sustained effort which may
+be infeasible. The suggestion is instead to have translations made for only
+a key set of pages from the core project website.
+
+## Detailed Description and Implementation
+
+The bulk of the work for setting up translation infrastructure, finding and
+vetting translators, and working out how to publish translations, will fall
+upon a cross-functional team funded by the [Scientific Python Community & Communications
+Infrastructure grant](https://scientific-python.org/doc/scientific-python-community-and-communications-infrastructure-2022.pdf)
+to work on adding translations for the main websites of all
+[Scientific Python core projects](https://scientific-python.org/specs/core-projects/).
+The hope is to minimize the burden on the core Pandas maintainers.
+
+No translated content would be hosted within the Pandas repository itself.
+Instead a separate GitHub repository could be set up containing the content
+selected for translation. This repository could then be synced to the Crowdin
+localization management platform as described in
+[Crowdin's documentation](https://support.crowdin.com/github-integration/).
+Crowdin would then provide a user interface for translators, and updates to
+translations would be pushed to a feature branch, with completed translations
+periodically merged into `main` after given approval by trusted
+language-specific admin's working across the Scientific Python core projects
+participating in the translation program. There will be no need for Pandas
+maintainers to verify the quality of translations.
+
+The result would be a repository containing parallel versions of content from
+pandas.pydata.org, translated into various languages. Translated content could
+then be pulled from this repository during generation of the Pandas website. A
+low friction means of choosing between languages could then be added. Possibly a
+drop-down language selector similar to what now exists for https://numpy.org, or
+simple links similar to what now exists for https://www.sympy.org/en/index.html.
+A developer supported by the "Scientific Python Community & Communications
+Infrastructure grant" could assist with making the changes necessary for the
+Pandas website to support publication of translations.
+
+If desired, a cron job could be set up on the repository containing translated
+content to check for relevant changes or updates to the Pandas website's content
+and pull them if necessary. Translators could then receive a notification from
+Crowdin that there are new strings to translate. This could help with the
+process of keeping translations up to date.
+
+
+### PDEP History
+
+- 01 February 2024: Initial draft
| Following up on the discussion in #56301, this PR adds a first draft for a PDEP proposing creation and publication of translations of the core website content.
To summarize, the [Scientific Python community and communications infrastructure grant](https://scientific-python.org/doc/scientific-python-community-and-communications-infrastructure-2022.pdf) is supporting work to facilitate official translation of content from the websites of [Scientific Python core projects](https://scientific-python.org/specs/core-projects/) and publication of these translations. This PDEP proposes that Pandas participates in these efforts. The hope is that the bulk of the work would be taken on by myself and/or a colleague at Quansight, and the work required by Pandas maintainers would be minimal.
There's more information in the PDEP below. Thank you. Looking forward to hearing your feedback. | https://api.github.com/repos/pandas-dev/pandas/pulls/57204 | 2024-02-02T01:53:51Z | 2024-02-27T19:09:50Z | null | 2024-02-27T19:09:50Z |
Backport PR #57174 on branch 2.2.x (BUG: Interchange protocol implementation allows non-string column names) | diff --git a/doc/source/whatsnew/v2.2.1.rst b/doc/source/whatsnew/v2.2.1.rst
index 56e645d4c55db..13d5024b5a131 100644
--- a/doc/source/whatsnew/v2.2.1.rst
+++ b/doc/source/whatsnew/v2.2.1.rst
@@ -32,6 +32,7 @@ Bug fixes
~~~~~~~~~
- Fixed bug in :func:`pandas.api.interchange.from_dataframe` which was raising for Nullable integers (:issue:`55069`)
- Fixed bug in :func:`pandas.api.interchange.from_dataframe` which was raising for empty inputs (:issue:`56700`)
+- Fixed bug in :func:`pandas.api.interchange.from_dataframe` which wasn't converting columns names to strings (:issue:`55069`)
- Fixed bug in :meth:`DataFrame.__getitem__` for empty :class:`DataFrame` with Copy-on-Write enabled (:issue:`57130`)
.. ---------------------------------------------------------------------------
diff --git a/pandas/core/interchange/column.py b/pandas/core/interchange/column.py
index 96757072dd854..e273ecad8b51e 100644
--- a/pandas/core/interchange/column.py
+++ b/pandas/core/interchange/column.py
@@ -77,6 +77,14 @@ def __init__(self, column: pd.Series, allow_copy: bool = True) -> None:
Note: doesn't deal with extension arrays yet, just assume a regular
Series/ndarray for now.
"""
+ if isinstance(column, pd.DataFrame):
+ raise TypeError(
+ "Expected a Series, got a DataFrame. This likely happened "
+ "because you called __dataframe__ on a DataFrame which, "
+ "after converting column names to string, resulted in duplicated "
+ f"names: {column.columns}. Please rename these columns before "
+ "using the interchange protocol."
+ )
if not isinstance(column, pd.Series):
raise NotImplementedError(f"Columns of type {type(column)} not handled yet")
diff --git a/pandas/core/interchange/dataframe.py b/pandas/core/interchange/dataframe.py
index 4f08b2c2b3a7b..1ffe0e8e8dbb0 100644
--- a/pandas/core/interchange/dataframe.py
+++ b/pandas/core/interchange/dataframe.py
@@ -32,7 +32,7 @@ def __init__(self, df: DataFrame, allow_copy: bool = True) -> None:
Constructor - an instance of this (private) class is returned from
`pd.DataFrame.__dataframe__`.
"""
- self._df = df
+ self._df = df.rename(columns=str, copy=False)
self._allow_copy = allow_copy
def __dataframe__(
diff --git a/pandas/tests/interchange/test_impl.py b/pandas/tests/interchange/test_impl.py
index 209944427d5dc..d47b533f92235 100644
--- a/pandas/tests/interchange/test_impl.py
+++ b/pandas/tests/interchange/test_impl.py
@@ -180,8 +180,6 @@ def test_missing_from_masked():
}
)
- df2 = df.__dataframe__()
-
rng = np.random.default_rng(2)
dict_null = {col: rng.integers(low=0, high=len(df)) for col in df.columns}
for col, num_nulls in dict_null.items():
@@ -382,6 +380,30 @@ def test_large_string():
tm.assert_frame_equal(result, expected)
+def test_non_str_names():
+ # https://github.com/pandas-dev/pandas/issues/56701
+ df = pd.Series([1, 2, 3], name=0).to_frame()
+ names = df.__dataframe__().column_names()
+ assert names == ["0"]
+
+
+def test_non_str_names_w_duplicates():
+ # https://github.com/pandas-dev/pandas/issues/56701
+ df = pd.DataFrame({"0": [1, 2, 3], 0: [4, 5, 6]})
+ dfi = df.__dataframe__()
+ with pytest.raises(
+ TypeError,
+ match=(
+ "Expected a Series, got a DataFrame. This likely happened because you "
+ "called __dataframe__ on a DataFrame which, after converting column "
+ r"names to string, resulted in duplicated names: Index\(\['0', '0'\], "
+ r"dtype='object'\). Please rename these columns before using the "
+ "interchange protocol."
+ ),
+ ):
+ pd.api.interchange.from_dataframe(dfi, allow_copy=False)
+
+
@pytest.mark.parametrize(
"dtype", ["Int8", pytest.param("Int8[pyarrow]", marks=td.skip_if_no("pyarrow"))]
)
| Backport PR #57174: BUG: Interchange protocol implementation allows non-string column names | https://api.github.com/repos/pandas-dev/pandas/pulls/57203 | 2024-02-02T01:25:51Z | 2024-02-02T02:38:20Z | 2024-02-02T02:38:20Z | 2024-02-02T02:38:20Z |
Backport PR #57163 on branch 2.2.x (CI: Add macOS M1 CI) | diff --git a/.github/workflows/unit-tests.yml b/.github/workflows/unit-tests.yml
index a3cffb4b03b93..4c7aa1e1e49ee 100644
--- a/.github/workflows/unit-tests.yml
+++ b/.github/workflows/unit-tests.yml
@@ -214,7 +214,8 @@ jobs:
timeout-minutes: 90
strategy:
matrix:
- os: [macos-latest, windows-latest]
+ # Note: Don't use macOS latest since macos 14 appears to be arm64 only
+ os: [macos-13, macos-14, windows-latest]
env_file: [actions-39.yaml, actions-310.yaml, actions-311.yaml, actions-312.yaml]
fail-fast: false
runs-on: ${{ matrix.os }}
@@ -227,8 +228,7 @@ jobs:
PANDAS_CI: 1
PYTEST_TARGET: pandas
PATTERN: "not slow and not db and not network and not single_cpu"
- # GH 47443: PYTEST_WORKERS > 0 crashes Windows builds with memory related errors
- PYTEST_WORKERS: ${{ matrix.os == 'macos-latest' && 'auto' || '0' }}
+ PYTEST_WORKERS: 'auto'
steps:
- name: Checkout
@@ -354,7 +354,8 @@ jobs:
strategy:
fail-fast: false
matrix:
- os: [ubuntu-22.04, macOS-latest, windows-latest]
+ # Separate out macOS 13 and 14, since macOS 14 is arm64 only
+ os: [ubuntu-22.04, macOS-13, macOS-14, windows-latest]
timeout-minutes: 90
diff --git a/.github/workflows/wheels.yml b/.github/workflows/wheels.yml
index 6d3b9048a2122..f79b2c51b5f92 100644
--- a/.github/workflows/wheels.yml
+++ b/.github/workflows/wheels.yml
@@ -94,7 +94,9 @@ jobs:
buildplat:
- [ubuntu-22.04, manylinux_x86_64]
- [ubuntu-22.04, musllinux_x86_64]
- - [macos-12, macosx_*]
+ - [macos-12, macosx_x86_64]
+ # Note: M1 images on Github Actions start from macOS 14
+ - [macos-14, macosx_arm64]
- [windows-2022, win_amd64]
# TODO: support PyPy?
python: [["cp39", "3.9"], ["cp310", "3.10"], ["cp311", "3.11"], ["cp312", "3.12"]]
@@ -128,7 +130,7 @@ jobs:
# Python version used to build sdist doesn't matter
# wheel will be built from sdist with the correct version
- name: Unzip sdist (macOS)
- if: ${{ matrix.buildplat[1] == 'macosx_*' }}
+ if: ${{ startsWith(matrix.buildplat[1], 'macosx') }}
run: |
tar -xzf ./dist/${{ env.sdist_name }} -C ./dist
@@ -139,18 +141,18 @@ jobs:
- name: Build normal wheels
if: ${{ (env.IS_SCHEDULE_DISPATCH != 'true' || env.IS_PUSH == 'true') }}
- uses: pypa/cibuildwheel@v2.16.4
+ uses: pypa/cibuildwheel@v2.16.5
with:
- package-dir: ./dist/${{ matrix.buildplat[1] == 'macosx_*' && env.sdist_name || needs.build_sdist.outputs.sdist_file }}
+ package-dir: ./dist/${{ startsWith(matrix.buildplat[1], 'macosx') && env.sdist_name || needs.build_sdist.outputs.sdist_file }}
env:
CIBW_PRERELEASE_PYTHONS: True
CIBW_BUILD: ${{ matrix.python[0] }}-${{ matrix.buildplat[1] }}
- name: Build nightly wheels (with NumPy pre-release)
if: ${{ (env.IS_SCHEDULE_DISPATCH == 'true' && env.IS_PUSH != 'true') }}
- uses: pypa/cibuildwheel@v2.16.4
+ uses: pypa/cibuildwheel@v2.16.5
with:
- package-dir: ./dist/${{ matrix.buildplat[1] == 'macosx_*' && env.sdist_name || needs.build_sdist.outputs.sdist_file }}
+ package-dir: ./dist/${{ startsWith(matrix.buildplat[1], 'macosx') && env.sdist_name || needs.build_sdist.outputs.sdist_file }}
env:
# The nightly wheels should be build witht he NumPy 2.0 pre-releases
# which requires the additional URL.
@@ -183,7 +185,7 @@ jobs:
$TST_CMD = @"
python -m pip install hypothesis>=6.46.1 pytest>=7.3.2 pytest-xdist>=2.2.0;
python -m pip install `$(Get-Item pandas\wheelhouse\*.whl);
- python -c `'import pandas as pd; pd.test(extra_args=[\"`\"--no-strict-data-files`\"\", \"`\"-m not clipboard and not single_cpu and not slow and not network and not db`\"\"])`';
+ python -c `'import pandas as pd; pd.test(extra_args=[`\"--no-strict-data-files`\", `\"-m not clipboard and not single_cpu and not slow and not network and not db`\"])`';
"@
# add rc to the end of the image name if the Python version is unreleased
docker pull python:${{ matrix.python[1] == '3.12' && '3.12-rc' || format('{0}-windowsservercore', matrix.python[1]) }}
@@ -191,7 +193,7 @@ jobs:
- uses: actions/upload-artifact@v4
with:
- name: ${{ matrix.python[0] }}-${{ startsWith(matrix.buildplat[1], 'macosx') && 'macosx' || matrix.buildplat[1] }}
+ name: ${{ matrix.python[0] }}-${{ matrix.buildplat[1] }}
path: ./wheelhouse/*.whl
- name: Upload wheels & sdist
diff --git a/ci/deps/actions-310.yaml b/ci/deps/actions-310.yaml
index 45f114322015b..a3e44e6373145 100644
--- a/ci/deps/actions-310.yaml
+++ b/ci/deps/actions-310.yaml
@@ -14,7 +14,6 @@ dependencies:
- pytest>=7.3.2
- pytest-cov
- pytest-xdist>=2.2.0
- - pytest-localserver>=0.7.1
- pytest-qt>=4.2.0
- boto3
@@ -61,3 +60,4 @@ dependencies:
- adbc-driver-postgresql>=0.8.0
- adbc-driver-sqlite>=0.8.0
- tzdata>=2022.7
+ - pytest-localserver>=0.7.1
diff --git a/ci/deps/actions-311.yaml b/ci/deps/actions-311.yaml
index d14686696e669..95cd1a4d46ef4 100644
--- a/ci/deps/actions-311.yaml
+++ b/ci/deps/actions-311.yaml
@@ -14,7 +14,6 @@ dependencies:
- pytest>=7.3.2
- pytest-cov
- pytest-xdist>=2.2.0
- - pytest-localserver>=0.7.1
- pytest-qt>=4.2.0
- boto3
@@ -60,4 +59,4 @@ dependencies:
- pip:
- adbc-driver-postgresql>=0.8.0
- adbc-driver-sqlite>=0.8.0
- - tzdata>=2022.7
+ - pytest-localserver>=0.7.1
diff --git a/ci/deps/actions-312.yaml b/ci/deps/actions-312.yaml
index 86aaf24b4e15c..a442ed6feeb5d 100644
--- a/ci/deps/actions-312.yaml
+++ b/ci/deps/actions-312.yaml
@@ -14,7 +14,6 @@ dependencies:
- pytest>=7.3.2
- pytest-cov
- pytest-xdist>=2.2.0
- - pytest-localserver>=0.7.1
- pytest-qt>=4.2.0
- boto3
@@ -61,3 +60,4 @@ dependencies:
- adbc-driver-postgresql>=0.8.0
- adbc-driver-sqlite>=0.8.0
- tzdata>=2022.7
+ - pytest-localserver>=0.7.1
diff --git a/ci/deps/actions-39.yaml b/ci/deps/actions-39.yaml
index 31ee74174cd46..b162a78e7f115 100644
--- a/ci/deps/actions-39.yaml
+++ b/ci/deps/actions-39.yaml
@@ -14,7 +14,6 @@ dependencies:
- pytest>=7.3.2
- pytest-cov
- pytest-xdist>=2.2.0
- - pytest-localserver>=0.7.1
- pytest-qt>=4.2.0
- boto3
@@ -61,3 +60,4 @@ dependencies:
- adbc-driver-postgresql>=0.8.0
- adbc-driver-sqlite>=0.8.0
- tzdata>=2022.7
+ - pytest-localserver>=0.7.1
diff --git a/pyproject.toml b/pyproject.toml
index 2f70ade7b3afe..8c9a79aa2b059 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -162,10 +162,6 @@ test-command = """
pd.test(extra_args=["-m not clipboard and single_cpu and not slow and not network and not db", "--no-strict-data-files"]);' \
"""
-[tool.cibuildwheel.macos]
-archs = "x86_64 arm64"
-test-skip = "*_arm64"
-
[tool.cibuildwheel.windows]
before-build = "pip install delvewheel"
repair-wheel-command = "delvewheel repair -w {dest_dir} {wheel}"
| Backport PR #57163: CI: Add macOS M1 CI | https://api.github.com/repos/pandas-dev/pandas/pulls/57202 | 2024-02-02T00:53:33Z | 2024-02-02T02:37:48Z | 2024-02-02T02:37:48Z | 2024-02-02T02:37:49Z |
CLN: pyproject.toml | diff --git a/pyproject.toml b/pyproject.toml
index 96e8ea90afa59..d9e5b89b0f141 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -498,20 +498,10 @@ filterwarnings = [
"ignore::ResourceWarning:asyncio",
# From plotting doctests
"ignore:More than 20 figures have been opened:RuntimeWarning",
- # Will be fixed in numba 0.56: https://github.com/numba/numba/issues/7758
- "ignore:`np.MachAr` is deprecated:DeprecationWarning:numba",
"ignore:.*urllib3:DeprecationWarning:botocore",
"ignore:Setuptools is replacing distutils.:UserWarning:_distutils_hack",
# https://github.com/PyTables/PyTables/issues/822
"ignore:a closed node found in the registry:UserWarning:tables",
- "ignore:`np.object` is a deprecated:DeprecationWarning:tables",
- "ignore:tostring:DeprecationWarning:tables",
- "ignore:distutils Version classes are deprecated:DeprecationWarning:pandas_datareader",
- "ignore:distutils Version classes are deprecated:DeprecationWarning:numexpr",
- "ignore:distutils Version classes are deprecated:DeprecationWarning:fastparquet",
- "ignore:distutils Version classes are deprecated:DeprecationWarning:fsspec",
- # Can be removed once https://github.com/numpy/numpy/pull/24794 is merged
- "ignore:.*In the future `np.long` will be defined as.*:FutureWarning",
]
junit_family = "xunit2"
markers = [
diff --git a/scripts/tests/data/deps_minimum.toml b/scripts/tests/data/deps_minimum.toml
index 0424920e5f446..ca1dc0c961c42 100644
--- a/scripts/tests/data/deps_minimum.toml
+++ b/scripts/tests/data/deps_minimum.toml
@@ -494,14 +494,11 @@ exclude_lines = [
"pragma: no cover",
# Don't complain about missing debug-only code:s
"def __repr__",
- "if self.debug",
# Don't complain if tests don't hit defensive assertion code:
"raise AssertionError",
"raise NotImplementedError",
"AbstractMethodError",
# Don't complain if non-runnable code isn't run:
- "if 0:",
- "if __name__ == .__main__.:",
"if TYPE_CHECKING:",
]
| null | https://api.github.com/repos/pandas-dev/pandas/pulls/57201 | 2024-02-02T00:42:31Z | 2024-02-02T17:45:00Z | 2024-02-02T17:45:00Z | 2024-02-02T17:45:03Z |
STY: Enable some passing rules | diff --git a/pandas/_libs/tslibs/vectorized.pyi b/pandas/_libs/tslibs/vectorized.pyi
index de19f592da62b..f377c2e26ab81 100644
--- a/pandas/_libs/tslibs/vectorized.pyi
+++ b/pandas/_libs/tslibs/vectorized.pyi
@@ -1,7 +1,5 @@
-"""
-For cython types that cannot be represented precisely, closest-available
-python equivalents are used, and the precise types kept as adjacent comments.
-"""
+# For cython types that cannot be represented precisely, closest-available
+# python equivalents are used, and the precise types kept as adjacent comments.
from datetime import tzinfo
import numpy as np
diff --git a/pandas/core/indexers/objects.py b/pandas/core/indexers/objects.py
index f2db4886a5590..5119089bac977 100644
--- a/pandas/core/indexers/objects.py
+++ b/pandas/core/indexers/objects.py
@@ -399,7 +399,7 @@ def get_window_bounds(
start_arrays = []
end_arrays = []
window_indices_start = 0
- for key, indices in self.groupby_indices.items():
+ for indices in self.groupby_indices.values():
index_array: np.ndarray | None
if self.index_array is not None:
diff --git a/pandas/io/formats/style_render.py b/pandas/io/formats/style_render.py
index 26c13e95fa280..80df46bf2336a 100644
--- a/pandas/io/formats/style_render.py
+++ b/pandas/io/formats/style_render.py
@@ -1996,7 +1996,7 @@ class Tooltips:
def __init__(
self,
- css_props: CSSProperties = [
+ css_props: CSSProperties = [ # noqa: B006
("visibility", "hidden"),
("position", "absolute"),
("z-index", 1),
diff --git a/pandas/io/json/_normalize.py b/pandas/io/json/_normalize.py
index de4033d5767e6..39fbce0b6901c 100644
--- a/pandas/io/json/_normalize.py
+++ b/pandas/io/json/_normalize.py
@@ -537,8 +537,8 @@ def _recursive_extract(data, path, seen_meta, level: int = 0) -> None:
if values.ndim > 1:
# GH 37782
values = np.empty((len(v),), dtype=object)
- for i, v in enumerate(v):
- values[i] = v
+ for i, val in enumerate(v):
+ values[i] = val
result[k] = values.repeat(lengths)
return result
diff --git a/pandas/tests/arrays/masked/test_arithmetic.py b/pandas/tests/arrays/masked/test_arithmetic.py
index f4b571ca627b3..ea018d2da4d26 100644
--- a/pandas/tests/arrays/masked/test_arithmetic.py
+++ b/pandas/tests/arrays/masked/test_arithmetic.py
@@ -55,15 +55,15 @@ def test_array_scalar_like_equivalence(data, all_arithmetic_operators):
scalar_array = pd.array([scalar] * len(data), dtype=data.dtype)
# TODO also add len-1 array (np.array([scalar], dtype=data.dtype.numpy_dtype))
- for scalar in [scalar, data.dtype.type(scalar)]:
+ for val in [scalar, data.dtype.type(scalar)]:
if is_bool_not_implemented(data, all_arithmetic_operators):
msg = "operator '.*' not implemented for bool dtypes"
with pytest.raises(NotImplementedError, match=msg):
- op(data, scalar)
+ op(data, val)
with pytest.raises(NotImplementedError, match=msg):
op(data, scalar_array)
else:
- result = op(data, scalar)
+ result = op(data, val)
expected = op(data, scalar_array)
tm.assert_extension_array_equal(result, expected)
@@ -214,13 +214,13 @@ def test_error_len_mismatch(data, all_arithmetic_operators):
msg = "operator '.*' not implemented for bool dtypes"
err = NotImplementedError
- for other in [other, np.array(other)]:
+ for val in [other, np.array(other)]:
with pytest.raises(err, match=msg):
- op(data, other)
+ op(data, val)
s = pd.Series(data)
with pytest.raises(err, match=msg):
- op(s, other)
+ op(s, val)
@pytest.mark.parametrize("op", ["__neg__", "__abs__", "__invert__"])
diff --git a/pandas/tests/copy_view/index/test_index.py b/pandas/tests/copy_view/index/test_index.py
index 49d756cf32d34..596379a3266fb 100644
--- a/pandas/tests/copy_view/index/test_index.py
+++ b/pandas/tests/copy_view/index/test_index.py
@@ -10,7 +10,7 @@
from pandas.tests.copy_view.util import get_array
-def index_view(index_data=[1, 2]):
+def index_view(index_data):
df = DataFrame({"a": index_data, "b": 1.5})
view = df[:]
df = df.set_index("a", drop=True)
@@ -142,7 +142,7 @@ def test_index_from_index(using_copy_on_write, warn_copy_on_write):
],
)
def test_index_ops(using_copy_on_write, func, request):
- idx, view_ = index_view()
+ idx, view_ = index_view([1, 2])
expected = idx.copy(deep=True)
if "astype" in request.node.callspec.id:
expected = expected.astype("Int64")
diff --git a/pandas/tests/frame/test_query_eval.py b/pandas/tests/frame/test_query_eval.py
index 0a869d8f94f47..0e29db3ca85df 100644
--- a/pandas/tests/frame/test_query_eval.py
+++ b/pandas/tests/frame/test_query_eval.py
@@ -949,8 +949,8 @@ def test_str_query_method(self, parser, engine):
ops = 2 * ([eq] + [ne])
msg = r"'(Not)?In' nodes are not implemented"
- for lhs, op, rhs in zip(lhs, ops, rhs):
- ex = f"{lhs} {op} {rhs}"
+ for lh, op_, rh in zip(lhs, ops, rhs):
+ ex = f"{lh} {op_} {rh}"
with pytest.raises(NotImplementedError, match=msg):
df.query(
ex,
@@ -990,8 +990,8 @@ def test_str_list_query_method(self, parser, engine):
ops = 2 * ([eq] + [ne])
msg = r"'(Not)?In' nodes are not implemented"
- for lhs, op, rhs in zip(lhs, ops, rhs):
- ex = f"{lhs} {op} {rhs}"
+ for lh, ops_, rh in zip(lhs, ops, rhs):
+ ex = f"{lh} {ops_} {rh}"
with pytest.raises(NotImplementedError, match=msg):
df.query(ex, engine=engine, parser=parser)
else:
diff --git a/pandas/tests/io/test_feather.py b/pandas/tests/io/test_feather.py
index f012fcadc5592..c8b5b690ae118 100644
--- a/pandas/tests/io/test_feather.py
+++ b/pandas/tests/io/test_feather.py
@@ -36,7 +36,9 @@ def check_external_error_on_write(self, df):
with tm.ensure_clean() as path:
to_feather(df, path)
- def check_round_trip(self, df, expected=None, write_kwargs={}, **read_kwargs):
+ def check_round_trip(self, df, expected=None, write_kwargs=None, **read_kwargs):
+ if write_kwargs is None:
+ write_kwargs = {}
if expected is None:
expected = df.copy()
diff --git a/pandas/tests/plotting/common.py b/pandas/tests/plotting/common.py
index 69120160699c2..5a46cdcb051b6 100644
--- a/pandas/tests/plotting/common.py
+++ b/pandas/tests/plotting/common.py
@@ -433,7 +433,7 @@ def _check_box_return_type(
raise AssertionError
-def _check_grid_settings(obj, kinds, kws={}):
+def _check_grid_settings(obj, kinds, kws=None):
# Make sure plot defaults to rcParams['axes.grid'] setting, GH 9792
import matplotlib as mpl
@@ -446,6 +446,8 @@ def is_grid_on():
return not (xoff and yoff)
+ if kws is None:
+ kws = {}
spndx = 1
for kind in kinds:
mpl.pyplot.subplot(1, 4 * len(kinds), spndx)
diff --git a/pandas/tests/reshape/test_pivot.py b/pandas/tests/reshape/test_pivot.py
index fbc3d2b8a7c35..f020fd45c87d9 100644
--- a/pandas/tests/reshape/test_pivot.py
+++ b/pandas/tests/reshape/test_pivot.py
@@ -890,10 +890,14 @@ def _check_output(
result,
values_col,
data,
- index=["A", "B"],
- columns=["C"],
+ index=None,
+ columns=None,
margins_col="All",
):
+ if index is None:
+ index = ["A", "B"]
+ if columns is None:
+ columns = ["C"]
col_margins = result.loc[result.index[:-1], margins_col]
expected_col_margins = data.groupby(index)[values_col].mean()
tm.assert_series_equal(col_margins, expected_col_margins, check_names=False)
diff --git a/pandas/tests/window/test_expanding.py b/pandas/tests/window/test_expanding.py
index 6d452b27f3654..ad59f9e52514e 100644
--- a/pandas/tests/window/test_expanding.py
+++ b/pandas/tests/window/test_expanding.py
@@ -178,9 +178,9 @@ def test_expanding_count_with_min_periods_exceeding_series_length(frame_or_serie
def test_iter_expanding_dataframe(df, expected, min_periods):
# GH 11704
df = DataFrame(df)
- expected = [DataFrame(values, index=index) for (values, index) in expected]
+ expecteds = [DataFrame(values, index=index) for (values, index) in expected]
- for expected, actual in zip(expected, df.expanding(min_periods)):
+ for expected, actual in zip(expecteds, df.expanding(min_periods)):
tm.assert_frame_equal(actual, expected)
@@ -197,9 +197,9 @@ def test_iter_expanding_dataframe(df, expected, min_periods):
)
def test_iter_expanding_series(ser, expected, min_periods):
# GH 11704
- expected = [Series(values, index=index) for (values, index) in expected]
+ expecteds = [Series(values, index=index) for (values, index) in expected]
- for expected, actual in zip(expected, ser.expanding(min_periods)):
+ for expected, actual in zip(expecteds, ser.expanding(min_periods)):
tm.assert_series_equal(actual, expected)
diff --git a/pandas/tests/window/test_rolling.py b/pandas/tests/window/test_rolling.py
index fda631987255a..85821ed2cfb6f 100644
--- a/pandas/tests/window/test_rolling.py
+++ b/pandas/tests/window/test_rolling.py
@@ -760,9 +760,9 @@ def test_rolling_count_default_min_periods_with_null_values(frame_or_series):
def test_iter_rolling_dataframe(df, expected, window, min_periods):
# GH 11704
df = DataFrame(df)
- expected = [DataFrame(values, index=index) for (values, index) in expected]
+ expecteds = [DataFrame(values, index=index) for (values, index) in expected]
- for expected, actual in zip(expected, df.rolling(window, min_periods=min_periods)):
+ for expected, actual in zip(expecteds, df.rolling(window, min_periods=min_periods)):
tm.assert_frame_equal(actual, expected)
@@ -805,10 +805,10 @@ def test_iter_rolling_on_dataframe(expected, window):
}
)
- expected = [
+ expecteds = [
DataFrame(values, index=df.loc[index, "C"]) for (values, index) in expected
]
- for expected, actual in zip(expected, df.rolling(window, on="C")):
+ for expected, actual in zip(expecteds, df.rolling(window, on="C")):
tm.assert_frame_equal(actual, expected)
@@ -856,9 +856,11 @@ def test_iter_rolling_on_dataframe_unordered():
)
def test_iter_rolling_series(ser, expected, window, min_periods):
# GH 11704
- expected = [Series(values, index=index) for (values, index) in expected]
+ expecteds = [Series(values, index=index) for (values, index) in expected]
- for expected, actual in zip(expected, ser.rolling(window, min_periods=min_periods)):
+ for expected, actual in zip(
+ expecteds, ser.rolling(window, min_periods=min_periods)
+ ):
tm.assert_series_equal(actual, expected)
@@ -904,11 +906,11 @@ def test_iter_rolling_datetime(expected, expected_index, window):
# GH 11704
ser = Series(range(5), index=date_range(start="2020-01-01", periods=5, freq="D"))
- expected = [
+ expecteds = [
Series(values, index=idx) for (values, idx) in zip(expected, expected_index)
]
- for expected, actual in zip(expected, ser.rolling(window)):
+ for expected, actual in zip(expecteds, ser.rolling(window)):
tm.assert_series_equal(actual, expected)
diff --git a/pyproject.toml b/pyproject.toml
index 96e8ea90afa59..eef5aeb6ea6fa 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -249,12 +249,6 @@ ignore = [
"E402",
# do not assign a lambda expression, use a def
"E731",
- # line break before binary operator
- # "W503", # not yet implemented
- # line break after binary operator
- # "W504", # not yet implemented
- # controversial
- "B006",
# controversial
"B007",
# controversial
@@ -263,18 +257,10 @@ ignore = [
"B009",
# getattr is used to side-step mypy
"B010",
- # tests use assert False
- "B011",
# tests use comparisons but not their returned value
"B015",
- # false positives
- "B019",
- # Loop control variable overrides iterable it iterates
- "B020",
# Function definition does not bind loop variable
"B023",
- # Functions defined inside a loop must not use variables redefined in the loop
- # "B301", # not yet implemented
# Only works with python >=3.10
"B905",
# Too many arguments to function call
@@ -289,14 +275,10 @@ ignore = [
"PLW2901",
# Global statements are discouraged
"PLW0603",
- # Docstrings should not be included in stubs
- "PYI021",
# Use `typing.NamedTuple` instead of `collections.namedtuple`
"PYI024",
# No builtin `eval()` allowed
"PGH001",
- # compare-to-empty-string
- "PLC1901",
# while int | float can be shortened to float, the former is more explicit
"PYI041",
# incorrect-dict-iterator, flags valid Series.items usage
| null | https://api.github.com/repos/pandas-dev/pandas/pulls/57200 | 2024-02-01T20:34:35Z | 2024-02-02T17:43:22Z | 2024-02-02T17:43:22Z | 2024-02-02T17:43:25Z |
TST/CLN: Assorted | diff --git a/pandas/tests/config/test_localization.py b/pandas/tests/config/test_localization.py
index 3907f557d1075..844f67cd2d0ea 100644
--- a/pandas/tests/config/test_localization.py
+++ b/pandas/tests/config/test_localization.py
@@ -15,7 +15,6 @@
import pandas as pd
_all_locales = get_locales()
-_current_locale = locale.setlocale(locale.LC_ALL) # getlocale() is wrong, see GH#46595
# Don't run any of these tests if we have no locales.
pytestmark = pytest.mark.skipif(not _all_locales, reason="Need locales")
diff --git a/pandas/tests/frame/indexing/test_xs.py b/pandas/tests/frame/indexing/test_xs.py
index 535137edd16cf..80b4635b94d3b 100644
--- a/pandas/tests/frame/indexing/test_xs.py
+++ b/pandas/tests/frame/indexing/test_xs.py
@@ -36,10 +36,7 @@ def four_level_index_dataframe():
class TestXS:
- def test_xs(
- self, float_frame, datetime_frame, using_copy_on_write, warn_copy_on_write
- ):
- float_frame_orig = float_frame.copy()
+ def test_xs(self, float_frame):
idx = float_frame.index[5]
xs = float_frame.xs(idx)
for item, value in xs.items():
@@ -48,6 +45,7 @@ def test_xs(
else:
assert value == float_frame[item][idx]
+ def test_xs_mixed(self):
# mixed-type xs
test_data = {"A": {"1": 1, "2": 2}, "B": {"1": "1", "2": "2", "3": "3"}}
frame = DataFrame(test_data)
@@ -56,11 +54,14 @@ def test_xs(
assert xs["A"] == 1
assert xs["B"] == "1"
+ def test_xs_dt_error(self, datetime_frame):
with pytest.raises(
KeyError, match=re.escape("Timestamp('1999-12-31 00:00:00')")
):
datetime_frame.xs(datetime_frame.index[0] - BDay())
+ def test_xs_other(self, float_frame, using_copy_on_write, warn_copy_on_write):
+ float_frame_orig = float_frame.copy()
# xs get column
series = float_frame.xs("A", axis=1)
expected = float_frame["A"]
diff --git a/pandas/tests/indexes/datetimes/test_constructors.py b/pandas/tests/indexes/datetimes/test_constructors.py
index 7fdf17c797213..97e768b348d55 100644
--- a/pandas/tests/indexes/datetimes/test_constructors.py
+++ b/pandas/tests/indexes/datetimes/test_constructors.py
@@ -1042,7 +1042,7 @@ def test_dti_constructor_with_non_nano_now_today(self):
# result may not exactly match [now, today] so we'll test it up to a tolerance.
# (it *may* match exactly due to rounding)
- tolerance = pd.Timedelta(microseconds=1)
+ tolerance = pd.Timedelta(seconds=1)
diff0 = result[0] - now.as_unit("s")
assert diff0 >= pd.Timedelta(0), f"The difference is {diff0}"
| Only notable change is bumping up the tolerance in `test_dti_constructor_with_non_nano_now_today` xref https://github.com/pandas-dev/pandas/actions/runs/7746334432/job/21124335580 | https://api.github.com/repos/pandas-dev/pandas/pulls/57199 | 2024-02-01T20:01:12Z | 2024-02-02T17:43:33Z | 2024-02-02T17:43:33Z | 2024-02-02T17:43:35Z |
Backport PR #57173: BUG: pandas int extension dtypes has no attribute… | diff --git a/doc/source/whatsnew/v2.2.1.rst b/doc/source/whatsnew/v2.2.1.rst
index 9002d9af2c602..56e645d4c55db 100644
--- a/doc/source/whatsnew/v2.2.1.rst
+++ b/doc/source/whatsnew/v2.2.1.rst
@@ -30,6 +30,7 @@ Fixed regressions
Bug fixes
~~~~~~~~~
+- Fixed bug in :func:`pandas.api.interchange.from_dataframe` which was raising for Nullable integers (:issue:`55069`)
- Fixed bug in :func:`pandas.api.interchange.from_dataframe` which was raising for empty inputs (:issue:`56700`)
- Fixed bug in :meth:`DataFrame.__getitem__` for empty :class:`DataFrame` with Copy-on-Write enabled (:issue:`57130`)
diff --git a/pandas/core/interchange/column.py b/pandas/core/interchange/column.py
index ee1b5cd34a7f7..96757072dd854 100644
--- a/pandas/core/interchange/column.py
+++ b/pandas/core/interchange/column.py
@@ -11,6 +11,7 @@
from pandas.core.dtypes.dtypes import (
ArrowDtype,
+ BaseMaskedDtype,
DatetimeTZDtype,
)
@@ -143,6 +144,8 @@ def _dtype_from_pandasdtype(self, dtype) -> tuple[DtypeKind, int, str, str]:
byteorder = dtype.numpy_dtype.byteorder
elif isinstance(dtype, DatetimeTZDtype):
byteorder = dtype.base.byteorder # type: ignore[union-attr]
+ elif isinstance(dtype, BaseMaskedDtype):
+ byteorder = dtype.numpy_dtype.byteorder
else:
byteorder = dtype.byteorder
diff --git a/pandas/tests/interchange/test_impl.py b/pandas/tests/interchange/test_impl.py
index c6365233728d2..209944427d5dc 100644
--- a/pandas/tests/interchange/test_impl.py
+++ b/pandas/tests/interchange/test_impl.py
@@ -9,6 +9,7 @@
is_platform_windows,
)
from pandas.compat.numpy import np_version_lt1p23
+import pandas.util._test_decorators as td
import pandas as pd
import pandas._testing as tm
@@ -381,6 +382,17 @@ def test_large_string():
tm.assert_frame_equal(result, expected)
+@pytest.mark.parametrize(
+ "dtype", ["Int8", pytest.param("Int8[pyarrow]", marks=td.skip_if_no("pyarrow"))]
+)
+def test_nullable_integers(dtype: str) -> None:
+ # https://github.com/pandas-dev/pandas/issues/55069
+ df = pd.DataFrame({"a": [1]}, dtype=dtype)
+ expected = pd.DataFrame({"a": [1]}, dtype="int8")
+ result = pd.api.interchange.from_dataframe(df.__dataframe__())
+ tm.assert_frame_equal(result, expected)
+
+
def test_empty_dataframe():
# https://github.com/pandas-dev/pandas/issues/56700
df = pd.DataFrame({"a": []}, dtype="int8")
| … byteorder
- [ ] closes #xxxx (Replace xxxx with the GitHub issue number)
- [ ] [Tests added and passed](https://pandas.pydata.org/pandas-docs/dev/development/contributing_codebase.html#writing-tests) if fixing a bug or adding a new feature
- [ ] All [code checks passed](https://pandas.pydata.org/pandas-docs/dev/development/contributing_codebase.html#pre-commit).
- [ ] Added [type annotations](https://pandas.pydata.org/pandas-docs/dev/development/contributing_codebase.html#type-hints) to new arguments/methods/functions.
- [ ] Added an entry in the latest `doc/source/whatsnew/vX.X.X.rst` file if fixing a bug or adding a new feature.
| https://api.github.com/repos/pandas-dev/pandas/pulls/57198 | 2024-02-01T19:45:08Z | 2024-02-01T21:15:39Z | 2024-02-01T21:15:39Z | 2024-02-01T21:15:40Z |
fix typo | diff --git a/pandas/core/window/rolling.py b/pandas/core/window/rolling.py
index 67885fcaec852..e4502c031b1fb 100644
--- a/pandas/core/window/rolling.py
+++ b/pandas/core/window/rolling.py
@@ -952,7 +952,7 @@ class Window(BaseWindow):
If ``'left'``, the last point in the window is excluded from calculations.
- If ``'both'``, the no points in the window are excluded from calculations.
+ If ``'both'``, no point in the window is excluded from calculations.
If ``'neither'``, the first and last points in the window are excluded
from calculations.
| - [ ] closes #xxxx (Replace xxxx with the GitHub issue number)
- [ ] [Tests added and passed](https://pandas.pydata.org/pandas-docs/dev/development/contributing_codebase.html#writing-tests) if fixing a bug or adding a new feature
- [ ] All [code checks passed](https://pandas.pydata.org/pandas-docs/dev/development/contributing_codebase.html#pre-commit).
- [ ] Added [type annotations](https://pandas.pydata.org/pandas-docs/dev/development/contributing_codebase.html#type-hints) to new arguments/methods/functions.
- [ ] Added an entry in the latest `doc/source/whatsnew/vX.X.X.rst` file if fixing a bug or adding a new feature.
| https://api.github.com/repos/pandas-dev/pandas/pulls/57194 | 2024-02-01T13:40:54Z | 2024-02-01T17:32:42Z | 2024-02-01T17:32:42Z | 2024-02-01T20:14:06Z |
CI: Pinning doc previewer action to current release | diff --git a/.github/workflows/comment-commands.yml b/.github/workflows/comment-commands.yml
index b24a689078d4e..da9f6ac8bff78 100644
--- a/.github/workflows/comment-commands.yml
+++ b/.github/workflows/comment-commands.yml
@@ -24,7 +24,7 @@ jobs:
concurrency:
group: ${{ github.actor }}-preview-docs
steps:
- - uses: pandas-dev/github-doc-previewer@master
+ - uses: pandas-dev/github-doc-previewer@v0.3.1
with:
previewer-server: "https://pandas.pydata.org/preview"
artifact-job: "Doc Build and Upload"
| I don't think we'll have any doc previewer release that won't be done for pandas, but pinning the action to avoid automatic upgrades anyway. | https://api.github.com/repos/pandas-dev/pandas/pulls/57190 | 2024-02-01T05:07:12Z | 2024-02-01T17:14:56Z | 2024-02-01T17:14:55Z | 2024-02-01T17:15:04Z |
DEPR: Enforce deprecation of groupby(..., axis=1) | diff --git a/doc/source/user_guide/groupby.rst b/doc/source/user_guide/groupby.rst
index a25e2ed179b80..2a4d7791322e5 100644
--- a/doc/source/user_guide/groupby.rst
+++ b/doc/source/user_guide/groupby.rst
@@ -247,7 +247,7 @@ GroupBy object attributes
~~~~~~~~~~~~~~~~~~~~~~~~~
The ``groups`` attribute is a dictionary whose keys are the computed unique groups
-and corresponding values are the axis labels belonging to each group. In the
+and corresponding values are the index labels belonging to each group. In the
above example we have:
.. ipython:: python
diff --git a/doc/source/user_guide/window.rst b/doc/source/user_guide/window.rst
index d997aa119b359..e25c4c2441920 100644
--- a/doc/source/user_guide/window.rst
+++ b/doc/source/user_guide/window.rst
@@ -79,9 +79,9 @@ which will first group the data by the specified keys and then perform a windowi
.. versionadded:: 1.3.0
Some windowing operations also support the ``method='table'`` option in the constructor which
-performs the windowing operation over an entire :class:`DataFrame` instead of a single column or row at a time.
-This can provide a useful performance benefit for a :class:`DataFrame` with many columns or rows
-(with the corresponding ``axis`` argument) or the ability to utilize other columns during the windowing
+performs the windowing operation over an entire :class:`DataFrame` instead of a single column at a time.
+This can provide a useful performance benefit for a :class:`DataFrame` with many columns
+or the ability to utilize other columns during the windowing
operation. The ``method='table'`` option can only be used if ``engine='numba'`` is specified
in the corresponding method call.
diff --git a/doc/source/whatsnew/v3.0.0.rst b/doc/source/whatsnew/v3.0.0.rst
index f316f6b44c1b4..c5ac2a800223b 100644
--- a/doc/source/whatsnew/v3.0.0.rst
+++ b/doc/source/whatsnew/v3.0.0.rst
@@ -102,6 +102,7 @@ Deprecations
Removal of prior version deprecations/changes
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- Removed :meth:`DataFrameGroupby.fillna` and :meth:`SeriesGroupBy.fillna` (:issue:`55719`)
+- 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`)
diff --git a/pandas/core/frame.py b/pandas/core/frame.py
index b3e18d6ceaddd..ca488190a8704 100644
--- a/pandas/core/frame.py
+++ b/pandas/core/frame.py
@@ -9050,7 +9050,6 @@ def update(
def groupby(
self,
by=None,
- axis: Axis | lib.NoDefault = lib.no_default,
level: IndexLabel | None = None,
as_index: bool = True,
sort: bool = True,
@@ -9058,25 +9057,6 @@ def groupby(
observed: bool | lib.NoDefault = lib.no_default,
dropna: bool = True,
) -> DataFrameGroupBy:
- if axis is not lib.no_default:
- axis = self._get_axis_number(axis)
- if axis == 1:
- warnings.warn(
- "DataFrame.groupby with axis=1 is deprecated. Do "
- "`frame.T.groupby(...)` without axis instead.",
- FutureWarning,
- stacklevel=find_stack_level(),
- )
- else:
- warnings.warn(
- "The 'axis' keyword in DataFrame.groupby is deprecated and "
- "will be removed in a future version.",
- FutureWarning,
- stacklevel=find_stack_level(),
- )
- else:
- axis = 0
-
from pandas.core.groupby.generic import DataFrameGroupBy
if level is None and by is None:
@@ -9085,7 +9065,6 @@ def groupby(
return DataFrameGroupBy(
obj=self,
keys=by,
- axis=axis,
level=level,
as_index=as_index,
sort=sort,
diff --git a/pandas/core/generic.py b/pandas/core/generic.py
index 037f809eaabca..2a50262a23f7a 100644
--- a/pandas/core/generic.py
+++ b/pandas/core/generic.py
@@ -9366,7 +9366,6 @@ def between_time(
def resample(
self,
rule,
- axis: Axis | lib.NoDefault = lib.no_default,
closed: Literal["right", "left"] | None = None,
label: Literal["right", "left"] | None = None,
convention: Literal["start", "end", "s", "e"] | lib.NoDefault = lib.no_default,
@@ -9389,13 +9388,6 @@ def resample(
----------
rule : DateOffset, Timedelta or str
The offset string or object representing target conversion.
- axis : {{0 or 'index', 1 or 'columns'}}, default 0
- Which axis to use for up- or down-sampling. For `Series` this parameter
- is unused and defaults to 0. Must be
- `DatetimeIndex`, `TimedeltaIndex` or `PeriodIndex`.
-
- .. deprecated:: 2.0.0
- Use frame.T.resample(...) instead.
closed : {{'right', 'left'}}, default None
Which side of bin interval is closed. The default is 'left'
for all frequency offsets except for 'ME', 'YE', 'QE', 'BME',
@@ -9707,25 +9699,6 @@ def resample(
"""
from pandas.core.resample import get_resampler
- if axis is not lib.no_default:
- axis = self._get_axis_number(axis)
- if axis == 1:
- warnings.warn(
- "DataFrame.resample with axis=1 is deprecated. Do "
- "`frame.T.resample(...)` without axis instead.",
- FutureWarning,
- stacklevel=find_stack_level(),
- )
- else:
- warnings.warn(
- f"The 'axis' keyword in {type(self).__name__}.resample is "
- "deprecated and will be removed in a future version.",
- FutureWarning,
- stacklevel=find_stack_level(),
- )
- else:
- axis = 0
-
if kind is not lib.no_default:
# GH#55895
warnings.warn(
@@ -9755,7 +9728,6 @@ def resample(
freq=rule,
label=label,
closed=closed,
- axis=axis,
kind=kind,
convention=convention,
key=on,
@@ -12526,33 +12498,10 @@ def rolling(
center: bool_t = False,
win_type: str | None = None,
on: str | None = None,
- axis: Axis | lib.NoDefault = lib.no_default,
closed: IntervalClosedType | None = None,
step: int | None = None,
method: str = "single",
) -> Window | Rolling:
- if axis is not lib.no_default:
- axis = self._get_axis_number(axis)
- name = "rolling"
- if axis == 1:
- warnings.warn(
- f"Support for axis=1 in {type(self).__name__}.{name} is "
- "deprecated and will be removed in a future version. "
- f"Use obj.T.{name}(...) instead",
- FutureWarning,
- stacklevel=find_stack_level(),
- )
- else:
- warnings.warn(
- f"The 'axis' keyword in {type(self).__name__}.{name} is "
- "deprecated and will be removed in a future version. "
- "Call the method without the axis keyword instead.",
- FutureWarning,
- stacklevel=find_stack_level(),
- )
- else:
- axis = 0
-
if win_type is not None:
return Window(
self,
@@ -12561,7 +12510,6 @@ def rolling(
center=center,
win_type=win_type,
on=on,
- axis=axis,
closed=closed,
step=step,
method=method,
@@ -12574,7 +12522,6 @@ def rolling(
center=center,
win_type=win_type,
on=on,
- axis=axis,
closed=closed,
step=step,
method=method,
@@ -12585,31 +12532,9 @@ def rolling(
def expanding(
self,
min_periods: int = 1,
- axis: Axis | lib.NoDefault = lib.no_default,
method: Literal["single", "table"] = "single",
) -> Expanding:
- if axis is not lib.no_default:
- axis = self._get_axis_number(axis)
- name = "expanding"
- if axis == 1:
- warnings.warn(
- f"Support for axis=1 in {type(self).__name__}.{name} is "
- "deprecated and will be removed in a future version. "
- f"Use obj.T.{name}(...) instead",
- FutureWarning,
- stacklevel=find_stack_level(),
- )
- else:
- warnings.warn(
- f"The 'axis' keyword in {type(self).__name__}.{name} is "
- "deprecated and will be removed in a future version. "
- "Call the method without the axis keyword instead.",
- FutureWarning,
- stacklevel=find_stack_level(),
- )
- else:
- axis = 0
- return Expanding(self, min_periods=min_periods, axis=axis, method=method)
+ return Expanding(self, min_periods=min_periods, method=method)
@final
@doc(ExponentialMovingWindow)
@@ -12622,32 +12547,9 @@ def ewm(
min_periods: int | None = 0,
adjust: bool_t = True,
ignore_na: bool_t = False,
- axis: Axis | lib.NoDefault = lib.no_default,
times: np.ndarray | DataFrame | Series | None = None,
method: Literal["single", "table"] = "single",
) -> ExponentialMovingWindow:
- if axis is not lib.no_default:
- axis = self._get_axis_number(axis)
- name = "ewm"
- if axis == 1:
- warnings.warn(
- f"Support for axis=1 in {type(self).__name__}.{name} is "
- "deprecated and will be removed in a future version. "
- f"Use obj.T.{name}(...) instead",
- FutureWarning,
- stacklevel=find_stack_level(),
- )
- else:
- warnings.warn(
- f"The 'axis' keyword in {type(self).__name__}.{name} is "
- "deprecated and will be removed in a future version. "
- "Call the method without the axis keyword instead.",
- FutureWarning,
- stacklevel=find_stack_level(),
- )
- else:
- axis = 0
-
return ExponentialMovingWindow(
self,
com=com,
@@ -12657,7 +12559,6 @@ def ewm(
min_periods=min_periods,
adjust=adjust,
ignore_na=ignore_na,
- axis=axis,
times=times,
method=method,
)
diff --git a/pandas/core/groupby/generic.py b/pandas/core/groupby/generic.py
index 1a23d237dca46..e0811c914864b 100644
--- a/pandas/core/groupby/generic.py
+++ b/pandas/core/groupby/generic.py
@@ -95,7 +95,6 @@
from pandas._typing import (
ArrayLike,
- AxisInt,
BlockManager,
CorrelationMethod,
IndexLabel,
@@ -446,9 +445,7 @@ def _aggregate_named(self, func, *args, **kwargs):
result = {}
initialized = False
- for name, group in self._grouper.get_iterator(
- self._obj_with_exclusions, axis=self.axis
- ):
+ for name, group in self._grouper.get_iterator(self._obj_with_exclusions):
# needed for pandas/tests/groupby/test_groupby.py::test_basic_aggregations
object.__setattr__(group, "name", name)
@@ -512,16 +509,12 @@ def transform(self, func, *args, engine=None, engine_kwargs=None, **kwargs):
func, *args, engine=engine, engine_kwargs=engine_kwargs, **kwargs
)
- def _cython_transform(
- self, how: str, numeric_only: bool = False, axis: AxisInt = 0, **kwargs
- ):
- assert axis == 0 # handled by caller
-
+ def _cython_transform(self, how: str, numeric_only: bool = False, **kwargs):
obj = self._obj_with_exclusions
try:
result = self._grouper._cython_operation(
- "transform", obj._values, how, axis, **kwargs
+ "transform", obj._values, how, 0, **kwargs
)
except NotImplementedError as err:
# e.g. test_groupby_raises_string
@@ -544,7 +537,7 @@ def _transform_general(
results = []
for name, group in self._grouper.get_iterator(
- self._obj_with_exclusions, axis=self.axis
+ self._obj_with_exclusions,
):
# this setattr is needed for test_transform_lambda_with_datetimetz
object.__setattr__(group, "name", name)
@@ -615,9 +608,7 @@ def true_and_notna(x) -> bool:
try:
indices = [
self._get_index(name)
- for name, group in self._grouper.get_iterator(
- self._obj_with_exclusions, axis=self.axis
- )
+ for name, group in self._grouper.get_iterator(self._obj_with_exclusions)
if true_and_notna(group)
]
except (ValueError, TypeError) as err:
@@ -928,7 +919,7 @@ def take(
0 rabbit mammal 15.0
>>> gb = df["name"].groupby([1, 1, 2, 2, 2])
- Take elements at positions 0 and 1 along the axis 0 in each group (default).
+ Take elements at rows 0 and 1 in each group.
>>> gb.take([0, 1])
1 4 falcon
@@ -947,7 +938,7 @@ def take(
1 monkey
Name: name, dtype: object
"""
- result = self._op_via_apply("take", indices=indices, axis=0, **kwargs)
+ result = self._op_via_apply("take", indices=indices, **kwargs)
return result
def skew(
@@ -1334,12 +1325,6 @@ def aggregate(self, func=None, *args, engine=None, engine_kwargs=None, **kwargs)
# can't return early
result = self._aggregate_frame(func, *args, **kwargs)
- elif self.axis == 1:
- # _aggregate_multiple_funcs does not allow self.axis == 1
- # Note: axis == 1 precludes 'not self.as_index', see __init__
- result = self._aggregate_frame(func)
- return result
-
else:
# try to treat as if we are passing a list
gba = GroupByApply(self, [func], args=(), kwargs={})
@@ -1385,8 +1370,6 @@ def _python_agg_general(self, func, *args, **kwargs):
return self._python_apply_general(f, self._selected_obj, is_agg=True)
obj = self._obj_with_exclusions
- if self.axis == 1:
- obj = obj.T
if not len(obj.columns):
# e.g. test_margins_no_values_no_cols
@@ -1408,15 +1391,13 @@ def _aggregate_frame(self, func, *args, **kwargs) -> DataFrame:
obj = self._obj_with_exclusions
result: dict[Hashable, NDFrame | np.ndarray] = {}
- for name, grp_df in self._grouper.get_iterator(obj, self.axis):
+ for name, grp_df in self._grouper.get_iterator(obj):
fres = func(grp_df, *args, **kwargs)
result[name] = fres
result_index = self._grouper.result_index
- other_ax = obj.axes[1 - self.axis]
- out = self.obj._constructor(result, index=other_ax, columns=result_index)
- if self.axis == 0:
- out = out.T
+ out = self.obj._constructor(result, index=obj.columns, columns=result_index)
+ out = out.T
return out
@@ -1516,18 +1497,13 @@ def _wrap_applied_output_series(
# vstack+constructor is faster than concat and handles MI-columns
stacked_values = np.vstack([np.asarray(v) for v in values])
- if self.axis == 0:
- index = key_index
- columns = first_not_none.index.copy()
- if columns.name is None:
- # GH6124 - propagate name of Series when it's consistent
- names = {v.name for v in values}
- if len(names) == 1:
- columns.name = next(iter(names))
- else:
- index = first_not_none.index
- columns = key_index
- stacked_values = stacked_values.T
+ index = key_index
+ columns = first_not_none.index.copy()
+ if columns.name is None:
+ # GH6124 - propagate name of Series when it's consistent
+ names = {v.name for v in values}
+ if len(names) == 1:
+ columns.name = next(iter(names))
if stacked_values.dtype == object:
# We'll have the DataFrame constructor do inference
@@ -1543,16 +1519,11 @@ def _cython_transform(
self,
how: str,
numeric_only: bool = False,
- axis: AxisInt = 0,
**kwargs,
) -> DataFrame:
- assert axis == 0 # handled by caller
-
- # With self.axis == 0, we have multi-block tests
+ # We have multi-block tests
# e.g. test_rank_min_int, test_cython_transform_frame
# test_transform_numeric_ret
- # With self.axis == 1, _get_data_to_aggregate does a transpose
- # so we always have a single block.
mgr: BlockManager = self._get_data_to_aggregate(
numeric_only=numeric_only, name=how
)
@@ -1565,7 +1536,6 @@ def arr_func(bvalues: ArrayLike) -> ArrayLike:
res_mgr = mgr.apply(arr_func)
res_df = self.obj._constructor_from_mgr(res_mgr, axes=res_mgr.axes)
- res_df = self._maybe_transpose_result(res_df)
return res_df
def _transform_general(self, func, engine, engine_kwargs, *args, **kwargs):
@@ -1577,7 +1547,7 @@ def _transform_general(self, func, engine, engine_kwargs, *args, **kwargs):
applied = []
obj = self._obj_with_exclusions
- gen = self._grouper.get_iterator(obj, axis=self.axis)
+ gen = self._grouper.get_iterator(obj)
fast_path, slow_path = self._define_paths(func, *args, **kwargs)
# Determine whether to use slow or fast path by evaluating on the first group.
@@ -1611,10 +1581,9 @@ def _transform_general(self, func, engine, engine_kwargs, *args, **kwargs):
res = _wrap_transform_general_frame(self.obj, group, res)
applied.append(res)
- concat_index = obj.columns if self.axis == 0 else obj.index
- other_axis = 1 if self.axis == 0 else 0 # switches between 0 & 1
- concatenated = concat(applied, axis=self.axis, verify_integrity=False)
- concatenated = concatenated.reindex(concat_index, axis=other_axis, copy=False)
+ concat_index = obj.columns
+ concatenated = concat(applied, axis=0, verify_integrity=False)
+ concatenated = concatenated.reindex(concat_index, axis=1, copy=False)
return self._set_result_index_ordered(concatenated)
__examples_dataframe_doc = dedent(
@@ -1682,12 +1651,12 @@ def _define_paths(self, func, *args, **kwargs):
if isinstance(func, str):
fast_path = lambda group: getattr(group, func)(*args, **kwargs)
slow_path = lambda group: group.apply(
- lambda x: getattr(x, func)(*args, **kwargs), axis=self.axis
+ lambda x: getattr(x, func)(*args, **kwargs), axis=0
)
else:
fast_path = lambda group: func(group, *args, **kwargs)
slow_path = lambda group: group.apply(
- lambda x: func(x, *args, **kwargs), axis=self.axis
+ lambda x: func(x, *args, **kwargs), axis=0
)
return fast_path, slow_path
@@ -1771,7 +1740,7 @@ def filter(self, func, dropna: bool = True, *args, **kwargs):
indices = []
obj = self._selected_obj
- gen = self._grouper.get_iterator(obj, axis=self.axis)
+ gen = self._grouper.get_iterator(obj)
for name, group in gen:
# 2023-02-27 no tests are broken this pinning, but it is documented in the
@@ -1799,9 +1768,6 @@ def filter(self, func, dropna: bool = True, *args, **kwargs):
return self._apply_filter(indices, dropna)
def __getitem__(self, key) -> DataFrameGroupBy | SeriesGroupBy:
- if self.axis == 1:
- # GH 37725
- raise ValueError("Cannot subset columns when using axis=1")
# per GH 23566
if isinstance(key, tuple) and len(key) > 1:
# if len == 1, then it becomes a SeriesGroupBy and this is actually
@@ -1831,7 +1797,6 @@ def _gotitem(self, key, ndim: int, subset=None):
return DataFrameGroupBy(
subset,
self.keys,
- axis=self.axis,
level=self.level,
grouper=self._grouper,
exclusions=self.exclusions,
@@ -1865,11 +1830,7 @@ def _get_data_to_aggregate(
self, *, numeric_only: bool = False, name: str | None = None
) -> BlockManager:
obj = self._obj_with_exclusions
- if self.axis == 1:
- mgr = obj.T._mgr
- else:
- mgr = obj._mgr
-
+ mgr = obj._mgr
if numeric_only:
mgr = mgr.get_numeric_data()
return mgr
@@ -1949,13 +1910,6 @@ def nunique(self, dropna: bool = True) -> DataFrame:
4 ham 5 x
5 ham 5 y
"""
-
- if self.axis != 0:
- # see test_groupby_crash_on_nunique
- return self._python_apply_general(
- lambda sgb: sgb.nunique(dropna), self._obj_with_exclusions, is_agg=True
- )
-
return self._apply_to_column_groupbys(lambda sgb: sgb.nunique(dropna))
def idxmax(
@@ -2250,7 +2204,7 @@ def take(
0 rabbit mammal 15.0
>>> gb = df.groupby([1, 1, 2, 2, 2])
- Take elements at positions 0 and 1 along the axis 0 (default).
+ Take elements at rows 0 and 1.
Note how the indices selected in the result do not correspond to
our input indices 0 and 1. That's because we are selecting the 0th
@@ -2283,7 +2237,7 @@ def take(
2 0 rabbit mammal 15.0
1 monkey mammal NaN
"""
- result = self._op_via_apply("take", indices=indices, axis=0, **kwargs)
+ result = self._op_via_apply("take", indices=indices, **kwargs)
return result
def skew(
diff --git a/pandas/core/groupby/groupby.py b/pandas/core/groupby/groupby.py
index 1f0e0567446c6..c4ae47348a64c 100644
--- a/pandas/core/groupby/groupby.py
+++ b/pandas/core/groupby/groupby.py
@@ -19,7 +19,6 @@ class providing the base-class of operations.
partial,
wraps,
)
-import inspect
from textwrap import dedent
from typing import (
TYPE_CHECKING,
@@ -47,8 +46,6 @@ class providing the base-class of operations.
from pandas._typing import (
AnyArrayLike,
ArrayLike,
- Axis,
- AxisInt,
DtypeObj,
FillnaOptions,
IndexLabel,
@@ -533,8 +530,7 @@ class providing the base-class of operations.
--------
%(klass)s.groupby.apply : Apply function ``func`` group-wise and combine
the results together.
-%(klass)s.groupby.aggregate : Aggregate using one or more
- operations over the specified axis.
+%(klass)s.groupby.aggregate : Aggregate using one or more operations.
%(klass)s.transform : Call ``func`` on self producing a %(klass)s with the
same axis shape as self.
@@ -576,7 +572,7 @@ class providing the base-class of operations.
%(example)s"""
_agg_template_series = """
-Aggregate using one or more operations over the specified axis.
+Aggregate using one or more operations.
Parameters
----------
@@ -638,8 +634,7 @@ class providing the base-class of operations.
and combine the results together.
{klass}.groupby.transform : Transforms the Series on each group
based on the given function.
-{klass}.aggregate : Aggregate using one or more
- operations over the specified axis.
+{klass}.aggregate : Aggregate using one or more operations.
Notes
-----
@@ -658,7 +653,7 @@ class providing the base-class of operations.
{examples}"""
_agg_template_frame = """
-Aggregate using one or more operations over the specified axis.
+Aggregate using one or more operations.
Parameters
----------
@@ -671,7 +666,7 @@ class providing the base-class of operations.
- function
- string function name
- list of functions and/or function names, e.g. ``[np.sum, 'mean']``
- - dict of axis labels -> functions, function names or list of such.
+ - dict of index labels -> functions, function names or list of such.
- None, in which case ``**kwargs`` are used with Named Aggregation. Here the
output has one column for each element in ``**kwargs``. The name of the
column is keyword, whereas the value determines the aggregation used to compute
@@ -717,8 +712,7 @@ class providing the base-class of operations.
and combine the results together.
{klass}.groupby.transform : Transforms the Series on each group
based on the given function.
-{klass}.aggregate : Aggregate using one or more
- operations over the specified axis.
+{klass}.aggregate : Aggregate using one or more operations.
Notes
-----
@@ -775,7 +769,6 @@ def f(self):
class BaseGroupBy(PandasObject, SelectionMixin[NDFrameT], GroupByIndexingMixin):
_hidden_attrs = PandasObject._hidden_attrs | {
"as_index",
- "axis",
"dropna",
"exclusions",
"grouper",
@@ -787,7 +780,6 @@ class BaseGroupBy(PandasObject, SelectionMixin[NDFrameT], GroupByIndexingMixin):
"sort",
}
- axis: AxisInt
_grouper: ops.BaseGrouper
keys: _KeysArgType | None = None
level: IndexLabel | None = None
@@ -1132,9 +1124,7 @@ def get_group(self, name) -> DataFrame | Series:
inds = self._get_index(name)
if not len(inds):
raise KeyError(name)
-
- indexer = inds if self.axis == 0 else (slice(None), inds)
- return self._selected_obj.iloc[indexer]
+ return self._selected_obj.iloc[inds]
@final
def __iter__(self) -> Iterator[tuple[Hashable, NDFrameT]]:
@@ -1210,7 +1200,7 @@ def __iter__(self) -> Iterator[tuple[Hashable, NDFrameT]]:
"""
keys = self.keys
level = self.level
- result = self._grouper.get_iterator(self._selected_obj, axis=self.axis)
+ result = self._grouper.get_iterator(self._selected_obj)
# error: Argument 1 to "len" has incompatible type "Hashable"; expected "Sized"
if is_list_like(level) and len(level) == 1: # type: ignore[arg-type]
# GH 51583
@@ -1247,7 +1237,6 @@ class GroupBy(BaseGroupBy[NDFrameT]):
Parameters
----------
obj : pandas object
- axis : int, default 0
level : int, default None
Level of MultiIndex
groupings : list of Grouping objects
@@ -1276,7 +1265,7 @@ class GroupBy(BaseGroupBy[NDFrameT]):
::
- grouped = obj.groupby(keys, axis=axis)
+ grouped = obj.groupby(keys)
for key, group in grouped:
# do something with the data
@@ -1308,7 +1297,6 @@ def __init__(
self,
obj: NDFrameT,
keys: _KeysArgType | None = None,
- axis: Axis = 0,
level: IndexLabel | None = None,
grouper: ops.BaseGrouper | None = None,
exclusions: frozenset[Hashable] | None = None,
@@ -1324,11 +1312,6 @@ def __init__(
assert isinstance(obj, NDFrame), type(obj)
self.level = level
-
- if not as_index:
- if axis != 0:
- raise ValueError("as_index=False only valid for axis=0")
-
self.as_index = as_index
self.keys = keys
self.sort = sort
@@ -1339,7 +1322,6 @@ def __init__(
grouper, exclusions, obj = get_grouper(
obj,
keys,
- axis=axis,
level=level,
sort=sort,
observed=False if observed is lib.no_default else observed,
@@ -1360,7 +1342,6 @@ def __init__(
self.observed = observed
self.obj = obj
- self.axis = obj._get_axis_number(axis)
self._grouper = grouper
self.exclusions = frozenset(exclusions) if exclusions else frozenset()
@@ -1374,35 +1355,10 @@ def __getattr__(self, attr: str):
f"'{type(self).__name__}' object has no attribute '{attr}'"
)
- @final
- def _deprecate_axis(self, axis: int, name: str) -> None:
- if axis == 1:
- warnings.warn(
- f"{type(self).__name__}.{name} with axis=1 is deprecated and "
- "will be removed in a future version. Operate on the un-grouped "
- "DataFrame instead",
- FutureWarning,
- stacklevel=find_stack_level(),
- )
- else:
- warnings.warn(
- f"The 'axis' keyword in {type(self).__name__}.{name} is deprecated "
- "and will be removed in a future version. "
- "Call without passing 'axis' instead.",
- FutureWarning,
- stacklevel=find_stack_level(),
- )
-
@final
def _op_via_apply(self, name: str, *args, **kwargs):
"""Compute the result of an operation by using GroupBy's apply."""
f = getattr(type(self._obj_with_exclusions), name)
- sig = inspect.signature(f)
-
- # a little trickery for aggregation functions that need an axis
- # argument
- if "axis" in sig.parameters:
- kwargs["axis"] = self.axis
def curried(x):
return f(x, *args, **kwargs)
@@ -1451,7 +1407,7 @@ def _concat_objects(
result = concat(
values,
- axis=self.axis,
+ axis=0,
keys=group_keys,
levels=group_levels,
names=group_names,
@@ -1461,12 +1417,12 @@ def _concat_objects(
# GH5610, returns a MI, with the first level being a
# range index
keys = list(range(len(values)))
- result = concat(values, axis=self.axis, keys=keys)
+ result = concat(values, axis=0, keys=keys)
elif not not_indexed_same:
- result = concat(values, axis=self.axis)
+ result = concat(values, axis=0)
- ax = self._selected_obj._get_axis(self.axis)
+ ax = self._selected_obj.index
if self.dropna:
labels = self._grouper.group_info[0]
mask = labels != -1
@@ -1478,16 +1434,16 @@ def _concat_objects(
# so we resort to this
# GH 14776, 30667
# TODO: can we reuse e.g. _reindex_non_unique?
- if ax.has_duplicates and not result.axes[self.axis].equals(ax):
+ if ax.has_duplicates and not result.axes[0].equals(ax):
# e.g. test_category_order_transformer
target = algorithms.unique1d(ax._values)
indexer, _ = result.index.get_indexer_non_unique(target)
- result = result.take(indexer, axis=self.axis)
+ result = result.take(indexer, axis=0)
else:
- result = result.reindex(ax, axis=self.axis, copy=False)
+ result = result.reindex(ax, axis=0, copy=False)
else:
- result = concat(values, axis=self.axis)
+ result = concat(values, axis=0)
if self.obj.ndim == 1:
name = self.obj.name
@@ -1508,22 +1464,22 @@ def _set_result_index_ordered(
# set the result index on the passed values object and
# return the new object, xref 8046
- obj_axis = self.obj._get_axis(self.axis)
+ index = self.obj.index
if self._grouper.is_monotonic and not self._grouper.has_dropped_na:
# shortcut if we have an already ordered grouper
- result = result.set_axis(obj_axis, axis=self.axis, copy=False)
+ result = result.set_axis(index, axis=0, copy=False)
return result
# row order is scrambled => sort the rows by position in original index
original_positions = Index(self._grouper.result_ilocs())
- result = result.set_axis(original_positions, axis=self.axis, copy=False)
- result = result.sort_index(axis=self.axis)
+ result = result.set_axis(original_positions, axis=0, copy=False)
+ result = result.sort_index(axis=0)
if self._grouper.has_dropped_na:
# Add back in any missing rows due to dropna - index here is integral
# with values referring to the row of the input so can use RangeIndex
- result = result.reindex(RangeIndex(len(obj_axis)), axis=self.axis)
- result = result.set_axis(obj_axis, axis=self.axis, copy=False)
+ result = result.reindex(RangeIndex(len(index)), axis=0)
+ result = result.set_axis(index, axis=0, copy=False)
return result
@@ -1559,17 +1515,6 @@ def _insert_inaxis_grouper(self, result: Series | DataFrame) -> DataFrame:
return result
- @final
- def _maybe_transpose_result(self, result: NDFrameT) -> NDFrameT:
- if self.axis == 1:
- # Only relevant for DataFrameGroupBy, no-op for SeriesGroupBy
- result = result.T
- if result.index.equals(self.obj.index):
- # Retain e.g. DatetimeIndex/TimedeltaIndex freq
- # e.g. test_groupby_crash_on_nunique
- result.index = self.obj.index.copy()
- return result
-
@final
def _wrap_aggregated_output(
self,
@@ -1607,10 +1552,7 @@ def _wrap_aggregated_output(
result.index = index
- # error: Argument 1 to "_maybe_transpose_result" of "GroupBy" has
- # incompatible type "Union[Series, DataFrame]"; expected "NDFrameT"
- res = self._maybe_transpose_result(result) # type: ignore[arg-type]
- return self._reindex_output(res, qs=qs)
+ return self._reindex_output(result, qs=qs)
def _wrap_applied_output(
self,
@@ -1630,7 +1572,7 @@ def _numba_prep(self, data: DataFrame):
sorted_index = self._grouper._sort_idx
sorted_ids = self._grouper._sorted_ids
- sorted_data = data.take(sorted_index, axis=self.axis).to_numpy()
+ sorted_data = data.take(sorted_index, axis=0).to_numpy()
# GH 46867
index_data = data.index
if isinstance(index_data, MultiIndex):
@@ -1666,8 +1608,6 @@ def _numba_agg_general(
raise NotImplementedError(
"as_index=False is not supported. Use .reset_index() instead."
)
- if self.axis == 1:
- raise NotImplementedError("axis=1 is not supported.")
data = self._obj_with_exclusions
df = data if data.ndim == 2 else data.to_frame()
@@ -1877,7 +1817,7 @@ def _python_apply_general(
Series or DataFrame
data after applying f
"""
- values, mutated = self._grouper.apply_groupwise(f, data, self.axis)
+ values, mutated = self._grouper.apply_groupwise(f, data)
if not_indexed_same is None:
not_indexed_same = mutated
@@ -1995,13 +1935,9 @@ def array_func(values: ArrayLike) -> ArrayLike:
if how in ["idxmin", "idxmax"]:
res = self._wrap_idxmax_idxmin(res)
out = self._wrap_aggregated_output(res)
- if self.axis == 1:
- out = out.infer_objects(copy=False)
return out
- def _cython_transform(
- self, how: str, numeric_only: bool = False, axis: AxisInt = 0, **kwargs
- ):
+ def _cython_transform(self, how: str, numeric_only: bool = False, **kwargs):
raise AbstractMethodError(self)
@final
@@ -2055,7 +1991,7 @@ def _wrap_transform_fast_result(self, result: NDFrameT) -> NDFrameT:
# for each col, reshape to size of original frame by take operation
ids, _, _ = self._grouper.group_info
- result = result.reindex(self._grouper.result_index, axis=self.axis, copy=False)
+ result = result.reindex(self._grouper.result_index, axis=0, copy=False)
if self.obj.ndim == 1:
# i.e. SeriesGroupBy
@@ -2063,15 +1999,14 @@ def _wrap_transform_fast_result(self, result: NDFrameT) -> NDFrameT:
output = obj._constructor(out, index=obj.index, name=obj.name)
else:
# `.size()` gives Series output on DataFrame input, need axis 0
- axis = 0 if result.ndim == 1 else self.axis
# GH#46209
# Don't convert indices: negative indices need to give rise
# to null values in the result
- new_ax = result.axes[axis].take(ids)
+ new_ax = result.index.take(ids)
output = result._reindex_with_indexers(
- {axis: (new_ax, ids)}, allow_dups=True, copy=False
+ {0: (new_ax, ids)}, allow_dups=True, copy=False
)
- output = output.set_axis(obj._get_axis(self.axis), axis=axis)
+ output = output.set_axis(obj.index, axis=0)
return output
# -----------------------------------------------------------------
@@ -2084,7 +2019,7 @@ def _apply_filter(self, indices, dropna):
else:
indices = np.sort(np.concatenate(indices))
if dropna:
- filtered = self._selected_obj.take(indices, axis=self.axis)
+ filtered = self._selected_obj.take(indices, axis=0)
else:
mask = np.empty(len(self._selected_obj.index), dtype=bool)
mask.fill(False)
@@ -2762,10 +2697,6 @@ def _value_counts(
SeriesGroupBy additionally supports a bins argument. See the docstring of
DataFrameGroupBy.value_counts for a description of arguments.
"""
- if self.axis == 1:
- raise NotImplementedError(
- "DataFrameGroupBy.value_counts only handles axis=0"
- )
name = "proportion" if normalize else "count"
df = self.obj
@@ -2808,7 +2739,6 @@ def _value_counts(
grouper, _, _ = get_grouper(
df,
key=key,
- axis=self.axis,
sort=self.sort,
observed=False,
dropna=dropna,
@@ -3386,7 +3316,7 @@ def first(
3 6.0 3
"""
- def first_compat(obj: NDFrameT, axis: AxisInt = 0):
+ def first_compat(obj: NDFrameT):
def first(x: Series):
"""Helper function for first item that isn't NA."""
arr = x.array[notna(x.array)]
@@ -3395,7 +3325,7 @@ def first(x: Series):
return arr[0]
if isinstance(obj, DataFrame):
- return obj.apply(first, axis=axis)
+ return obj.apply(first)
elif isinstance(obj, Series):
return first(obj)
else: # pragma: no cover
@@ -3455,7 +3385,7 @@ def last(
3 6.0 3
"""
- def last_compat(obj: NDFrameT, axis: AxisInt = 0):
+ def last_compat(obj: NDFrameT):
def last(x: Series):
"""Helper function for last item that isn't NA."""
arr = x.array[notna(x.array)]
@@ -3464,7 +3394,7 @@ def last(x: Series):
return arr[-1]
if isinstance(obj, DataFrame):
- return obj.apply(last, axis=axis)
+ return obj.apply(last)
elif isinstance(obj, Series):
return last(obj)
else: # pragma: no cover
@@ -3595,8 +3525,6 @@ def describe(
obj,
not_indexed_same=True,
)
- if self.axis == 1:
- return result.T
# GH#49256 - properly handle the grouping column(s)
result = result.unstack()
@@ -3795,13 +3723,6 @@ def rolling(self, *args, **kwargs) -> RollingGroupby:
Provided integer column is ignored and excluded from result since
an integer index is not used to calculate the rolling window.
- axis : int or str, default 0
- If ``0`` or ``'index'``, roll across the rows.
-
- If ``1`` or ``'columns'``, roll across the columns.
-
- For `Series` this parameter is unused and defaults to 0.
-
closed : str, default None
If ``'right'``, the first point in the window is excluded from calculations.
@@ -3992,12 +3913,6 @@ def blk_func(values: ArrayLike) -> ArrayLike:
res_mgr = mgr.apply(blk_func)
new_obj = self._wrap_agged_manager(res_mgr)
-
- if self.axis == 1:
- # Only relevant for DataFrameGroupBy
- new_obj = new_obj.T
- new_obj.columns = self.obj.columns
-
new_obj.index = self.obj.index
return new_obj
@@ -4299,7 +4214,7 @@ def _nth(
# old behaviour, but with all and any support for DataFrames.
# modified in GH 7559 to have better perf
n = cast(int, n)
- dropped = self._selected_obj.dropna(how=dropna, axis=self.axis)
+ dropped = self._selected_obj.dropna(how=dropna, axis=0)
# get a new grouper for our dropped obj
grouper: np.ndarray | Index | ops.BaseGrouper
@@ -4320,10 +4235,7 @@ def _nth(
values = np.where(nulls, NA, grouper) # type: ignore[call-overload]
grouper = Index(values, dtype="Int64")
- if self.axis == 1:
- grb = dropped.T.groupby(grouper, as_index=self.as_index, sort=self.sort)
- else:
- grb = dropped.groupby(grouper, as_index=self.as_index, sort=self.sort)
+ grb = dropped.groupby(grouper, as_index=self.as_index, sort=self.sort)
return grb.nth(n)
@final
@@ -4376,12 +4288,8 @@ def quantile(
"""
mgr = self._get_data_to_aggregate(numeric_only=numeric_only, name="quantile")
obj = self._wrap_agged_manager(mgr)
- if self.axis == 1:
- splitter = self._grouper._get_splitter(obj.T, axis=self.axis)
- sdata = splitter._sorted_data.T
- else:
- splitter = self._grouper._get_splitter(obj, axis=self.axis)
- sdata = splitter._sorted_data
+ splitter = self._grouper._get_splitter(obj)
+ sdata = splitter._sorted_data
starts, ends = lib.generate_slices(splitter._slabels, splitter.ngroups)
@@ -4619,7 +4527,7 @@ def ngroup(self, ascending: bool = True):
dtype: int64
"""
obj = self._obj_with_exclusions
- index = obj._get_axis(self.axis)
+ index = obj.index
comp_ids = self._grouper.group_info[0]
dtype: type
@@ -4693,7 +4601,7 @@ def cumcount(self, ascending: bool = True):
5 0
dtype: int64
"""
- index = self._obj_with_exclusions._get_axis(self.axis)
+ index = self._obj_with_exclusions.index
cumcounts = self._cumcount_array(ascending=ascending)
return self._obj_1d_constructor(cumcounts, index)
@@ -4780,7 +4688,6 @@ def rank(
return self._cython_transform(
"rank",
numeric_only=False,
- axis=0,
**kwargs,
)
@@ -5127,7 +5034,7 @@ def shift(
obj = self._obj_with_exclusions
shifted = obj._reindex_with_indexers(
- {self.axis: (obj.axes[self.axis], res_indexer)},
+ {0: (obj.index, res_indexer)},
fill_value=fill_value,
allow_dups=True,
)
@@ -5332,13 +5239,8 @@ def pct_change(
fill_method = "ffill"
limit = 0
filled = getattr(self, fill_method)(limit=limit)
- if self.axis == 0:
- fill_grp = filled.groupby(self._grouper.codes, group_keys=self.group_keys)
- else:
- fill_grp = filled.T.groupby(self._grouper.codes, group_keys=self.group_keys)
+ fill_grp = filled.groupby(self._grouper.codes, group_keys=self.group_keys)
shifted = fill_grp.shift(periods=periods, freq=freq)
- if self.axis == 1:
- shifted = shifted.T
return (filled / shifted) - 1
@final
@@ -5425,7 +5327,7 @@ def tail(self, n: int = 5) -> NDFrameT:
@final
def _mask_selected_obj(self, mask: npt.NDArray[np.bool_]) -> NDFrameT:
"""
- Return _selected_obj with mask applied to the correct axis.
+ Return _selected_obj with mask applied.
Parameters
----------
@@ -5439,11 +5341,7 @@ def _mask_selected_obj(self, mask: npt.NDArray[np.bool_]) -> NDFrameT:
"""
ids = self._grouper.group_info[0]
mask = mask & (ids != -1)
-
- if self.axis == 0:
- return self._selected_obj[mask]
- else:
- return self._selected_obj.iloc[:, mask]
+ return self._selected_obj[mask]
@final
def _reindex_output(
@@ -5506,12 +5404,7 @@ def _reindex_output(
if self.as_index:
# Always holds for SeriesGroupBy unless GH#36507 is implemented
- d = {
- self.obj._get_axis_name(self.axis): index,
- "copy": False,
- "fill_value": fill_value,
- }
- return output.reindex(**d) # type: ignore[arg-type]
+ return output.reindex(index=index, copy=False, fill_value=fill_value)
# GH 13204
# Here, the categorical in-axis groupers, which need to be fully
@@ -5642,13 +5535,11 @@ def sample(
return self._selected_obj
size = sample.process_sampling_size(n, frac, replace)
if weights is not None:
- weights_arr = sample.preprocess_weights(
- self._selected_obj, weights, axis=self.axis
- )
+ weights_arr = sample.preprocess_weights(self._selected_obj, weights, axis=0)
random_state = com.random_state(random_state)
- group_iterator = self._grouper.get_iterator(self._selected_obj, self.axis)
+ group_iterator = self._grouper.get_iterator(self._selected_obj)
sampled_indices = []
for labels, obj in group_iterator:
@@ -5670,7 +5561,7 @@ def sample(
sampled_indices.append(grp_indices[grp_sample])
sampled_indices = np.concatenate(sampled_indices)
- return self._selected_obj.take(sampled_indices, axis=self.axis)
+ return self._selected_obj.take(sampled_indices, axis=0)
def _idxmax_idxmin(
self,
@@ -5747,7 +5638,7 @@ def _idxmax_idxmin(
return result
def _wrap_idxmax_idxmin(self, res: NDFrameT) -> NDFrameT:
- index = self.obj._get_axis(self.axis)
+ index = self.obj.index
if res.size == 0:
result = res.astype(index.dtype)
else:
@@ -5778,7 +5669,6 @@ def _wrap_idxmax_idxmin(self, res: NDFrameT) -> NDFrameT:
def get_groupby(
obj: NDFrame,
by: _KeysArgType | None = None,
- axis: AxisInt = 0,
grouper: ops.BaseGrouper | None = None,
group_keys: bool = True,
) -> GroupBy:
@@ -5797,7 +5687,6 @@ def get_groupby(
return klass(
obj=obj,
keys=by,
- axis=axis,
grouper=grouper,
group_keys=group_keys,
)
diff --git a/pandas/core/groupby/grouper.py b/pandas/core/groupby/grouper.py
index 08ee786170674..4b9cf5ab75525 100644
--- a/pandas/core/groupby/grouper.py
+++ b/pandas/core/groupby/grouper.py
@@ -17,7 +17,6 @@
warn_copy_on_write,
)
-from pandas._libs import lib
from pandas._libs.tslibs import OutOfBoundsDatetime
from pandas.errors import InvalidIndexError
from pandas.util._decorators import cache_readonly
@@ -55,7 +54,6 @@
from pandas._typing import (
ArrayLike,
- Axis,
NDFrameT,
npt,
)
@@ -68,10 +66,10 @@ class Grouper:
A Grouper allows the user to specify a groupby instruction for an object.
This specification will select a column via the key parameter, or if the
- level and/or axis parameters are given, a level of the index of the target
+ level parameter is given, a level of the index of the target
object.
- If `axis` and/or `level` are passed as keywords to both `Grouper` and
+ If ``level`` is passed as a keyword to both `Grouper` and
`groupby`, the values passed to `Grouper` take precedence.
Parameters
@@ -85,8 +83,6 @@ class Grouper:
(via key or level) is a datetime-like object. For full specification
of available frequencies, please see `here
<https://pandas.pydata.org/pandas-docs/stable/user_guide/timeseries.html#offset-aliases>`_.
- axis : str, int, defaults to 0
- Number/name of the axis.
sort : bool, default to False
Whether to sort the resulting labels.
closed : {'left' or 'right'}
@@ -249,7 +245,7 @@ class Grouper:
_gpr_index: Index | None
_grouper: Index | None
- _attributes: tuple[str, ...] = ("key", "level", "freq", "axis", "sort", "dropna")
+ _attributes: tuple[str, ...] = ("key", "level", "freq", "sort", "dropna")
def __new__(cls, *args, **kwargs):
if kwargs.get("freq") is not None:
@@ -263,29 +259,12 @@ def __init__(
key=None,
level=None,
freq=None,
- axis: Axis | lib.NoDefault = lib.no_default,
sort: bool = False,
dropna: bool = True,
) -> None:
- if type(self) is Grouper:
- # i.e. not TimeGrouper
- if axis is not lib.no_default:
- warnings.warn(
- "Grouper axis keyword is deprecated and will be removed in a "
- "future version. To group on axis=1, use obj.T.groupby(...) "
- "instead",
- FutureWarning,
- stacklevel=find_stack_level(),
- )
- else:
- axis = 0
- if axis is lib.no_default:
- axis = 0
-
self.key = key
self.level = level
self.freq = freq
- self.axis = axis
self.sort = sort
self.dropna = dropna
@@ -315,7 +294,6 @@ def _get_grouper(
grouper, _, obj = get_grouper(
obj,
[self.key],
- axis=self.axis,
level=self.level,
sort=self.sort,
validate=validate,
@@ -381,7 +359,7 @@ def _set_grouper(
ax = Index(obj[key], name=key)
else:
- ax = obj._get_axis(self.axis)
+ ax = obj.index
if self.level is not None:
level = self.level
@@ -404,7 +382,7 @@ def _set_grouper(
kind="mergesort", na_position="first"
)
ax = ax.take(indexer)
- obj = obj.take(indexer, axis=self.axis)
+ obj = obj.take(indexer, axis=0)
# error: Incompatible types in assignment (expression has type
# "NDFrameT", variable has type "None")
@@ -846,7 +824,6 @@ def groups(self) -> dict[Hashable, np.ndarray]:
def get_grouper(
obj: NDFrameT,
key=None,
- axis: Axis = 0,
level=None,
sort: bool = True,
observed: bool = False,
@@ -862,8 +839,8 @@ def get_grouper(
Groupers are ultimately index mappings. They can originate as:
index mappings, keys to columns, functions, or Groupers
- Groupers enable local references to axis,level,sort, while
- the passed in axis, level, and sort are 'global'.
+ Groupers enable local references to level,sort, while
+ the passed in level, and sort are 'global'.
This routine tries to figure out what the passing in references
are and then creates a Grouping for each one, combined into
@@ -875,10 +852,10 @@ def get_grouper(
If validate, then check for key/level overlaps.
"""
- group_axis = obj._get_axis(axis)
+ group_axis = obj.index
# validate that the passed single level is compatible with the passed
- # axis of the object
+ # index of the object
if level is not None:
# TODO: These if-block and else-block are almost same.
# MultiIndex instance check is removable, but it seems that there are
@@ -911,11 +888,8 @@ def get_grouper(
raise ValueError("multiple levels only valid with MultiIndex")
if isinstance(level, str):
- if obj._get_axis(axis).name != level:
- raise ValueError(
- f"level name {level} is not the name "
- f"of the {obj._get_axis_name(axis)}"
- )
+ if obj.index.name != level:
+ raise ValueError(f"level name {level} is not the name of the index")
elif level > 0 or level < -1:
raise ValueError("level > 0 or level < -1 only valid with MultiIndex")
@@ -1028,14 +1002,14 @@ def is_in_obj(gpr) -> bool:
elif is_in_axis(gpr): # df.groupby('name')
if obj.ndim != 1 and gpr in obj:
if validate:
- obj._check_label_or_level_ambiguity(gpr, axis=axis)
+ obj._check_label_or_level_ambiguity(gpr, axis=0)
in_axis, name, gpr = True, gpr, obj[gpr]
if gpr.ndim != 1:
# non-unique columns; raise here to get the name in the
# exception message
raise ValueError(f"Grouper for '{name}' not 1-dimensional")
exclusions.add(name)
- elif obj._is_level_reference(gpr, axis=axis):
+ elif obj._is_level_reference(gpr, axis=0):
in_axis, level, gpr = False, gpr, None
else:
raise KeyError(gpr)
diff --git a/pandas/core/groupby/ops.py b/pandas/core/groupby/ops.py
index e2ddf9aa5c0c1..632ff7356d1c7 100644
--- a/pandas/core/groupby/ops.py
+++ b/pandas/core/groupby/ops.py
@@ -604,9 +604,7 @@ def __iter__(self) -> Iterator[Hashable]:
def nkeys(self) -> int:
return len(self.groupings)
- def get_iterator(
- self, data: NDFrameT, axis: AxisInt = 0
- ) -> Iterator[tuple[Hashable, NDFrameT]]:
+ def get_iterator(self, data: NDFrameT) -> Iterator[tuple[Hashable, NDFrameT]]:
"""
Groupby iterator
@@ -615,12 +613,12 @@ def get_iterator(
Generator yielding sequence of (name, subsetted object)
for each group
"""
- splitter = self._get_splitter(data, axis=axis)
+ splitter = self._get_splitter(data)
keys = self.group_keys_seq
yield from zip(keys, splitter)
@final
- def _get_splitter(self, data: NDFrame, axis: AxisInt = 0) -> DataSplitter:
+ def _get_splitter(self, data: NDFrame) -> DataSplitter:
"""
Returns
-------
@@ -633,7 +631,6 @@ def _get_splitter(self, data: NDFrame, axis: AxisInt = 0) -> DataSplitter:
ngroups,
sorted_ids=self._sorted_ids,
sort_idx=self._sort_idx,
- axis=axis,
)
@final
@@ -879,7 +876,7 @@ def _aggregate_series_pure_python(
result = np.empty(ngroups, dtype="O")
initialized = False
- splitter = self._get_splitter(obj, axis=0)
+ splitter = self._get_splitter(obj)
for i, group in enumerate(splitter):
res = func(group)
@@ -896,10 +893,10 @@ def _aggregate_series_pure_python(
@final
def apply_groupwise(
- self, f: Callable, data: DataFrame | Series, axis: AxisInt = 0
+ self, f: Callable, data: DataFrame | Series
) -> tuple[list, bool]:
mutated = False
- splitter = self._get_splitter(data, axis=axis)
+ splitter = self._get_splitter(data)
group_keys = self.group_keys_seq
result_values = []
@@ -917,7 +914,7 @@ def apply_groupwise(
# group might be modified
group_axes = group.axes
res = f(group)
- if not mutated and not _is_indexed_like(res, group_axes, axis):
+ if not mutated and not _is_indexed_like(res, group_axes):
mutated = True
result_values.append(res)
# getattr pattern for __name__ is needed for functools.partial objects
@@ -1024,7 +1021,7 @@ def codes_info(self) -> npt.NDArray[np.intp]:
ids = ids[sorter]
return ids
- def get_iterator(self, data: NDFrame, axis: AxisInt = 0):
+ def get_iterator(self, data: NDFrame):
"""
Groupby iterator
@@ -1033,12 +1030,7 @@ def get_iterator(self, data: NDFrame, axis: AxisInt = 0):
Generator yielding sequence of (name, subsetted object)
for each group
"""
- if axis == 0:
- slicer = lambda start, edge: data.iloc[start:edge]
- else:
- slicer = lambda start, edge: data.iloc[:, start:edge]
-
- length = len(data.axes[axis])
+ slicer = lambda start, edge: data.iloc[start:edge]
start = 0
for edge, label in zip(self.bins, self.binlabels):
@@ -1046,7 +1038,7 @@ def get_iterator(self, data: NDFrame, axis: AxisInt = 0):
yield label, slicer(start, edge)
start = edge
- if start < length:
+ if start < len(data):
yield self.binlabels[-1], slicer(start, None)
@cache_readonly
@@ -1110,13 +1102,13 @@ def groupings(self) -> list[grouper.Grouping]:
return [ping]
-def _is_indexed_like(obj, axes, axis: AxisInt) -> bool:
+def _is_indexed_like(obj, axes) -> bool:
if isinstance(obj, Series):
if len(axes) > 1:
return False
- return obj.axes[axis].equals(axes[axis])
+ return obj.index.equals(axes[0])
elif isinstance(obj, DataFrame):
- return obj.axes[axis].equals(axes[axis])
+ return obj.index.equals(axes[0])
return False
@@ -1134,7 +1126,6 @@ def __init__(
*,
sort_idx: npt.NDArray[np.intp],
sorted_ids: npt.NDArray[np.intp],
- axis: AxisInt = 0,
) -> None:
self.data = data
self.labels = ensure_platform_int(labels) # _should_ already be np.intp
@@ -1143,9 +1134,6 @@ def __init__(
self._slabels = sorted_ids
self._sort_idx = sort_idx
- self.axis = axis
- assert isinstance(axis, int), axis
-
def __iter__(self) -> Iterator:
sdata = self._sorted_data
@@ -1161,7 +1149,7 @@ def __iter__(self) -> Iterator:
@cache_readonly
def _sorted_data(self) -> NDFrameT:
- return self.data.take(self._sort_idx, axis=self.axis)
+ return self.data.take(self._sort_idx, axis=0)
def _chop(self, sdata, slice_obj: slice) -> NDFrame:
raise AbstractMethodError(self)
@@ -1179,11 +1167,8 @@ def _chop(self, sdata: Series, slice_obj: slice) -> Series:
class FrameSplitter(DataSplitter):
def _chop(self, sdata: DataFrame, slice_obj: slice) -> DataFrame:
# Fastpath equivalent to:
- # if self.axis == 0:
- # return sdata.iloc[slice_obj]
- # else:
- # return sdata.iloc[:, slice_obj]
- mgr = sdata._mgr.get_slice(slice_obj, axis=1 - self.axis)
+ # return sdata.iloc[slice_obj]
+ mgr = sdata._mgr.get_slice(slice_obj, axis=1)
df = sdata._constructor_from_mgr(mgr, axes=mgr.axes)
return df.__finalize__(sdata, method="groupby")
@@ -1195,7 +1180,6 @@ def _get_splitter(
*,
sort_idx: npt.NDArray[np.intp],
sorted_ids: npt.NDArray[np.intp],
- axis: AxisInt = 0,
) -> DataSplitter:
if isinstance(data, Series):
klass: type[DataSplitter] = SeriesSplitter
@@ -1203,6 +1187,4 @@ def _get_splitter(
# i.e. DataFrame
klass = FrameSplitter
- return klass(
- data, labels, ngroups, sort_idx=sort_idx, sorted_ids=sorted_ids, axis=axis
- )
+ return klass(data, labels, ngroups, sort_idx=sort_idx, sorted_ids=sorted_ids)
diff --git a/pandas/core/resample.py b/pandas/core/resample.py
index 4d6507d89ec90..bf5b7e5906180 100644
--- a/pandas/core/resample.py
+++ b/pandas/core/resample.py
@@ -101,7 +101,6 @@
Any,
AnyArrayLike,
Axis,
- AxisInt,
Concatenate,
Frequency,
IndexLabel,
@@ -134,7 +133,6 @@ class Resampler(BaseGroupBy, PandasObject):
----------
obj : Series or DataFrame
groupby : TimeGrouper
- axis : int, default 0
kind : str or None
'period', 'timestamp' to override default index treatment
@@ -156,7 +154,6 @@ class Resampler(BaseGroupBy, PandasObject):
# to the groupby descriptor
_attributes = [
"freq",
- "axis",
"closed",
"label",
"convention",
@@ -169,7 +166,6 @@ def __init__(
self,
obj: NDFrame,
timegrouper: TimeGrouper,
- axis: Axis = 0,
kind=None,
*,
gpr_index: Index,
@@ -180,7 +176,6 @@ def __init__(
self._timegrouper = timegrouper
self.keys = None
self.sort = True
- self.axis = obj._get_axis_number(axis)
self.kind = kind
self.group_keys = group_keys
self.as_index = True
@@ -449,7 +444,7 @@ def _gotitem(self, key, ndim: int, subset=None):
assert subset.ndim == 1
grouped = get_groupby(
- subset, by=None, grouper=grouper, axis=self.axis, group_keys=self.group_keys
+ subset, by=None, grouper=grouper, group_keys=self.group_keys
)
return grouped
@@ -462,9 +457,7 @@ def _groupby_and_aggregate(self, how, *args, **kwargs):
# Excludes `on` column when provided
obj = self._obj_with_exclusions
- grouped = get_groupby(
- obj, by=None, grouper=grouper, axis=self.axis, group_keys=self.group_keys
- )
+ grouped = get_groupby(obj, by=None, grouper=grouper, group_keys=self.group_keys)
try:
if callable(how):
@@ -1801,12 +1794,7 @@ def _downsample(self, how, **kwargs):
# we are downsampling
# we want to call the actual grouper method here
- if self.axis == 0:
- result = obj.groupby(self._grouper).aggregate(how, **kwargs)
- else:
- # test_resample_axis1
- result = obj.T.groupby(self._grouper).aggregate(how, **kwargs).T
-
+ result = obj.groupby(self._grouper).aggregate(how, **kwargs)
return self._wrap_result(result)
def _adjust_binner_for_upsample(self, binner):
@@ -1837,8 +1825,6 @@ def _upsample(self, method, limit: int | None = None, fill_value=None):
.fillna: Fill NA/NaN values using the specified method.
"""
- if self.axis:
- raise AssertionError("axis must be 0")
if self._from_selection:
raise ValueError(
"Upsampling from level= or on= selection "
@@ -2010,7 +1996,6 @@ def _upsample(self, method, limit: int | None = None, fill_value=None):
obj,
indexer,
new_index,
- axis=self.axis,
)
return self._wrap_result(new_obj)
@@ -2131,7 +2116,6 @@ def __init__(
closed: Literal["left", "right"] | None = None,
label: Literal["left", "right"] | None = None,
how: str = "mean",
- axis: Axis = 0,
fill_method=None,
limit: int | None = None,
kind: str | None = None,
@@ -2228,7 +2212,7 @@ def __init__(
# always sort time groupers
kwargs["sort"] = True
- super().__init__(freq=freq, key=key, axis=axis, **kwargs)
+ super().__init__(freq=freq, key=key, **kwargs)
def _get_resampler(self, obj: NDFrame, kind=None) -> Resampler:
"""
@@ -2255,7 +2239,6 @@ def _get_resampler(self, obj: NDFrame, kind=None) -> Resampler:
obj,
timegrouper=self,
kind=kind,
- axis=self.axis,
group_keys=self.group_keys,
gpr_index=ax,
)
@@ -2279,7 +2262,6 @@ def _get_resampler(self, obj: NDFrame, kind=None) -> Resampler:
obj,
timegrouper=self,
kind=kind,
- axis=self.axis,
group_keys=self.group_keys,
gpr_index=ax,
)
@@ -2287,7 +2269,6 @@ def _get_resampler(self, obj: NDFrame, kind=None) -> Resampler:
return TimedeltaIndexResampler(
obj,
timegrouper=self,
- axis=self.axis,
group_keys=self.group_keys,
gpr_index=ax,
)
@@ -2559,7 +2540,9 @@ def _set_grouper(
def _take_new_index(
- obj: NDFrameT, indexer: npt.NDArray[np.intp], new_index: Index, axis: AxisInt = 0
+ obj: NDFrameT,
+ indexer: npt.NDArray[np.intp],
+ new_index: Index,
) -> NDFrameT:
if isinstance(obj, ABCSeries):
new_values = algos.take_nd(obj._values, indexer)
@@ -2568,8 +2551,6 @@ def _take_new_index(
new_values, index=new_index, name=obj.name
)
elif isinstance(obj, ABCDataFrame):
- if axis == 1:
- raise NotImplementedError("axis 1 is not supported")
new_mgr = obj._mgr.reindex_indexer(new_axis=new_index, indexer=indexer, axis=1)
return obj._constructor_from_mgr(new_mgr, axes=new_mgr.axes)
else:
diff --git a/pandas/core/reshape/merge.py b/pandas/core/reshape/merge.py
index 22304bbdd1575..e53eea6f7f075 100644
--- a/pandas/core/reshape/merge.py
+++ b/pandas/core/reshape/merge.py
@@ -264,7 +264,7 @@ def _groupby_and_merge(
if all(item in right.columns for item in by):
rby = right.groupby(by, sort=False)
- for key, lhs in lby._grouper.get_iterator(lby._selected_obj, axis=lby.axis):
+ for key, lhs in lby._grouper.get_iterator(lby._selected_obj):
if rby is None:
rhs = right
else:
diff --git a/pandas/core/reshape/pivot.py b/pandas/core/reshape/pivot.py
index ff973f6defc09..db28bfb1e9200 100644
--- a/pandas/core/reshape/pivot.py
+++ b/pandas/core/reshape/pivot.py
@@ -472,7 +472,7 @@ def _all_key():
margin_keys.append(all_key)
else:
- margin = data.groupby(level=0, axis=0, observed=observed).apply(aggfunc)
+ margin = data.groupby(level=0, observed=observed).apply(aggfunc)
all_key = _all_key()
table[all_key] = margin
result = table
diff --git a/pandas/core/series.py b/pandas/core/series.py
index 7b56facc7a91f..657b384c57235 100644
--- a/pandas/core/series.py
+++ b/pandas/core/series.py
@@ -2205,7 +2205,6 @@ def _set_name(
def groupby(
self,
by=None,
- axis: Axis = 0,
level: IndexLabel | None = None,
as_index: bool = True,
sort: bool = True,
@@ -2219,12 +2218,10 @@ def groupby(
raise TypeError("You have to supply one of 'by' and 'level'")
if not as_index:
raise TypeError("as_index=False only valid with DataFrame")
- axis = self._get_axis_number(axis)
return SeriesGroupBy(
obj=self,
keys=by,
- axis=axis,
level=level,
as_index=as_index,
sort=sort,
diff --git a/pandas/core/shared_docs.py b/pandas/core/shared_docs.py
index 49b380e0af01e..9d693f034d911 100644
--- a/pandas/core/shared_docs.py
+++ b/pandas/core/shared_docs.py
@@ -102,15 +102,6 @@
the values are used as-is to determine the groups. A label or list
of labels may be passed to group by the columns in ``self``.
Notice that a tuple is interpreted as a (single) key.
-axis : {0 or 'index', 1 or 'columns'}, default 0
- Split along rows (0) or columns (1). For `Series` this parameter
- is unused and defaults to 0.
-
- .. deprecated:: 2.1.0
-
- Will be removed and behave like axis=0 in a future version.
- For ``axis=1``, do ``frame.T.groupby(...)`` instead.
-
level : int, level name, or sequence of such, default None
If the axis is a MultiIndex (hierarchical), group by a particular
level or levels. Do not specify both ``by`` and ``level``.
diff --git a/pandas/core/window/ewm.py b/pandas/core/window/ewm.py
index 9ebf32d3e536e..3c07fc156aea1 100644
--- a/pandas/core/window/ewm.py
+++ b/pandas/core/window/ewm.py
@@ -56,7 +56,6 @@
if TYPE_CHECKING:
from pandas._typing import (
- Axis,
TimedeltaConvertibleTypes,
npt,
)
@@ -204,13 +203,6 @@ class ExponentialMovingWindow(BaseWindow):
[:math:`x_0`, None, :math:`x_2`] are :math:`1-\alpha` and :math:`1` if
``adjust=True``, and :math:`1-\alpha` and :math:`\alpha` if ``adjust=False``.
- axis : {0, 1}, default 0
- If ``0`` or ``'index'``, calculate across the rows.
-
- If ``1`` or ``'columns'``, calculate across the columns.
-
- For `Series` this parameter is unused and defaults to 0.
-
times : np.ndarray, Series, default None
Only applicable to ``mean()``.
@@ -328,7 +320,6 @@ class ExponentialMovingWindow(BaseWindow):
"min_periods",
"adjust",
"ignore_na",
- "axis",
"times",
"method",
]
@@ -343,7 +334,6 @@ def __init__(
min_periods: int | None = 0,
adjust: bool = True,
ignore_na: bool = False,
- axis: Axis = 0,
times: np.ndarray | NDFrame | None = None,
method: str = "single",
*,
@@ -356,7 +346,6 @@ def __init__(
center=False,
closed=None,
method=method,
- axis=axis,
selection=selection,
)
self.com = com
@@ -397,9 +386,7 @@ def __init__(
"times is not None."
)
# Without times, points are equally spaced
- self._deltas = np.ones(
- max(self.obj.shape[self.axis] - 1, 0), dtype=np.float64
- )
+ self._deltas = np.ones(max(self.obj.shape[0] - 1, 0), dtype=np.float64)
self._com = get_center_of_mass(
# error: Argument 3 to "get_center_of_mass" has incompatible type
# "Union[float, Any, None, timedelta64, signedinteger[_64Bit]]";
@@ -460,7 +447,6 @@ def online(
min_periods=self.min_periods,
adjust=self.adjust,
ignore_na=self.ignore_na,
- axis=self.axis,
times=self.times,
engine=engine,
engine_kwargs=engine_kwargs,
@@ -941,7 +927,6 @@ def __init__(
min_periods: int | None = 0,
adjust: bool = True,
ignore_na: bool = False,
- axis: Axis = 0,
times: np.ndarray | NDFrame | None = None,
engine: str = "numba",
engine_kwargs: dict[str, bool] | None = None,
@@ -961,13 +946,10 @@ def __init__(
min_periods=min_periods,
adjust=adjust,
ignore_na=ignore_na,
- axis=axis,
times=times,
selection=selection,
)
- self._mean = EWMMeanState(
- self._com, self.adjust, self.ignore_na, self.axis, obj.shape
- )
+ self._mean = EWMMeanState(self._com, self.adjust, self.ignore_na, obj.shape)
if maybe_use_numba(engine):
self.engine = engine
self.engine_kwargs = engine_kwargs
@@ -1055,7 +1037,7 @@ def mean(self, *args, update=None, update_times=None, **kwargs):
if update_times is not None:
raise NotImplementedError("update_times is not implemented.")
update_deltas = np.ones(
- max(self._selected_obj.shape[self.axis - 1] - 1, 0), dtype=np.float64
+ max(self._selected_obj.shape[-1] - 1, 0), dtype=np.float64
)
if update is not None:
if self._mean.last_ewm is None:
diff --git a/pandas/core/window/expanding.py b/pandas/core/window/expanding.py
index aac10596ffc69..1bf26c482337c 100644
--- a/pandas/core/window/expanding.py
+++ b/pandas/core/window/expanding.py
@@ -36,7 +36,6 @@
if TYPE_CHECKING:
from pandas._typing import (
- Axis,
QuantileInterpolation,
WindowingRankType,
)
@@ -58,13 +57,6 @@ class Expanding(RollingAndExpandingMixin):
Minimum number of observations in window required to have a value;
otherwise, result is ``np.nan``.
- axis : int or str, default 0
- If ``0`` or ``'index'``, roll across the rows.
-
- If ``1`` or ``'columns'``, roll across the columns.
-
- For `Series` this parameter is unused and defaults to 0.
-
method : str {'single', 'table'}, default 'single'
Execute the rolling operation per single column or row (``'single'``)
or over the entire object (``'table'``).
@@ -119,20 +111,18 @@ class Expanding(RollingAndExpandingMixin):
4 7.0
"""
- _attributes: list[str] = ["min_periods", "axis", "method"]
+ _attributes: list[str] = ["min_periods", "method"]
def __init__(
self,
obj: NDFrame,
min_periods: int = 1,
- axis: Axis = 0,
method: str = "single",
selection=None,
) -> None:
super().__init__(
obj=obj,
min_periods=min_periods,
- axis=axis,
method=method,
selection=selection,
)
diff --git a/pandas/core/window/numba_.py b/pandas/core/window/numba_.py
index 9357945e78c63..eb06479fc325e 100644
--- a/pandas/core/window/numba_.py
+++ b/pandas/core/window/numba_.py
@@ -186,8 +186,7 @@ def generate_numba_table_func(
Generate a numba jitted function to apply window calculations table-wise.
Func will be passed a M window size x N number of columns array, and
- must return a 1 x N number of columns array. Func is intended to operate
- row-wise, but the result will be transposed for axis=1.
+ must return a 1 x N number of columns array.
1. jit the user's function
2. Return a rolling apply function with the jitted function inline
diff --git a/pandas/core/window/online.py b/pandas/core/window/online.py
index 29d1f740e021f..72236bf5ccea2 100644
--- a/pandas/core/window/online.py
+++ b/pandas/core/window/online.py
@@ -87,15 +87,14 @@ def online_ewma(
class EWMMeanState:
- def __init__(self, com, adjust, ignore_na, axis, shape) -> None:
+ def __init__(self, com, adjust, ignore_na, shape) -> None:
alpha = 1.0 / (1.0 + com)
- self.axis = axis
self.shape = shape
self.adjust = adjust
self.ignore_na = ignore_na
self.new_wt = 1.0 if adjust else alpha
self.old_wt_factor = 1.0 - alpha
- self.old_wt = np.ones(self.shape[self.axis - 1])
+ self.old_wt = np.ones(self.shape[-1])
self.last_ewm = None
def run_ewm(self, weighted_avg, deltas, min_periods, ewm_func):
@@ -114,5 +113,5 @@ def run_ewm(self, weighted_avg, deltas, min_periods, ewm_func):
return result
def reset(self) -> None:
- self.old_wt = np.ones(self.shape[self.axis - 1])
+ self.old_wt = np.ones(self.shape[-1])
self.last_ewm = None
diff --git a/pandas/core/window/rolling.py b/pandas/core/window/rolling.py
index 67885fcaec852..dddd0a7ac48c3 100644
--- a/pandas/core/window/rolling.py
+++ b/pandas/core/window/rolling.py
@@ -100,7 +100,6 @@
from pandas._typing import (
ArrayLike,
- Axis,
NDFrameT,
QuantileInterpolation,
WindowingRankType,
@@ -131,7 +130,6 @@ def __init__(
min_periods: int | None = None,
center: bool | None = False,
win_type: str | None = None,
- axis: Axis = 0,
on: str | Index | None = None,
closed: str | None = None,
step: int | None = None,
@@ -147,15 +145,10 @@ def __init__(
self.min_periods = min_periods
self.center = center
self.win_type = win_type
- self.axis = obj._get_axis_number(axis) if axis is not None else None
self.method = method
self._win_freq_i8: int | None = None
if self.on is None:
- if self.axis == 0:
- self._on = self.obj.index
- else:
- # i.e. self.axis == 1
- self._on = self.obj.columns
+ self._on = self.obj.index
elif isinstance(self.on, Index):
self._on = self.on
elif isinstance(self.obj, ABCDataFrame) and self.on in self.obj.columns:
@@ -278,14 +271,8 @@ def _create_data(self, obj: NDFrameT, numeric_only: bool = False) -> NDFrameT:
# filter out the on from the object
if self.on is not None and not isinstance(self.on, Index) and obj.ndim == 2:
obj = obj.reindex(columns=obj.columns.difference([self.on]), copy=False)
- if obj.ndim > 1 and (numeric_only or self.axis == 1):
- # GH: 20649 in case of mixed dtype and axis=1 we have to convert everything
- # to float to calculate the complete row at once. We exclude all non-numeric
- # dtypes.
+ if obj.ndim > 1 and numeric_only:
obj = self._make_numeric_only(obj)
- if self.axis == 1:
- obj = obj.astype("float64", copy=False)
- obj._mgr = obj._mgr.consolidate()
return obj
def _gotitem(self, key, ndim, subset=None):
@@ -477,9 +464,6 @@ def _apply_columnwise(
obj = notna(obj).astype(int)
obj._mgr = obj._mgr.consolidate()
- if self.axis == 1:
- obj = obj.T
-
taker = []
res_values = []
for i, arr in enumerate(obj._iter_column_arrays()):
@@ -505,9 +489,6 @@ def _apply_columnwise(
verify_integrity=False,
)
- if self.axis == 1:
- df = df.T
-
return self._resolve_output(df, obj)
def _apply_tablewise(
@@ -523,9 +504,7 @@ def _apply_tablewise(
raise ValueError("method='table' not applicable for Series objects.")
obj = self._create_data(self._selected_obj, numeric_only)
values = self._prep_values(obj.to_numpy())
- values = values.T if self.axis == 1 else values
result = homogeneous_func(values)
- result = result.T if self.axis == 1 else result
index = self._slice_axis_for_step(obj.index, result)
columns = (
obj.columns
@@ -633,8 +612,6 @@ def _numba_apply(
else window_indexer.window_size
)
obj = self._create_data(self._selected_obj)
- if self.axis == 1:
- obj = obj.T
values = self._prep_values(obj.to_numpy())
if values.ndim == 1:
values = values.reshape(-1, 1)
@@ -660,7 +637,6 @@ def _numba_apply(
result = aggregator(
values.T, start=start, end=end, min_periods=min_periods, **func_kwargs
).T
- result = result.T if self.axis == 1 else result
index = self._slice_axis_for_step(obj.index, result)
if obj.ndim == 1:
result = result.squeeze()
@@ -935,18 +911,6 @@ class Window(BaseWindow):
Provided integer column is ignored and excluded from result since
an integer index is not used to calculate the rolling window.
- axis : int or str, default 0
- If ``0`` or ``'index'``, roll across the rows.
-
- If ``1`` or ``'columns'``, roll across the columns.
-
- For `Series` this parameter is unused and defaults to 0.
-
- .. deprecated:: 2.1.0
-
- The axis keyword is deprecated. For ``axis=1``,
- transpose the DataFrame first instead.
-
closed : str, default None
If ``'right'``, the first point in the window is excluded from calculations.
@@ -1138,7 +1102,6 @@ class Window(BaseWindow):
"min_periods",
"center",
"win_type",
- "axis",
"on",
"closed",
"step",
@@ -1858,7 +1821,6 @@ class Rolling(RollingAndExpandingMixin):
"min_periods",
"center",
"win_type",
- "axis",
"on",
"closed",
"step",
@@ -1926,10 +1888,7 @@ def _validate_datetimelike_monotonic(self) -> None:
def _raise_monotonic_error(self, msg: str):
on = self.on
if on is None:
- if self.axis == 0:
- on = "index"
- else:
- on = "column"
+ on = "index"
raise ValueError(f"{on} {msg}")
@doc(
diff --git a/pandas/plotting/_matplotlib/boxplot.py b/pandas/plotting/_matplotlib/boxplot.py
index 084452ec23719..b41e03d87b275 100644
--- a/pandas/plotting/_matplotlib/boxplot.py
+++ b/pandas/plotting/_matplotlib/boxplot.py
@@ -544,12 +544,7 @@ def boxplot_frame_groupby(
maybe_adjust_figure(fig, bottom=0.15, top=0.9, left=0.1, right=0.9, wspace=0.2)
else:
keys, frames = zip(*grouped)
- if grouped.axis == 0:
- df = pd.concat(frames, keys=keys, axis=1)
- elif len(frames) > 1:
- df = frames[0].join(frames[1::])
- else:
- df = frames[0]
+ df = pd.concat(frames, keys=keys, axis=1)
# GH 16748, DataFrameGroupby fails when subplots=False and `column` argument
# is assigned, and in this case, since `df` here becomes MI after groupby,
diff --git a/pandas/tests/apply/test_str.py b/pandas/tests/apply/test_str.py
index e9967b75becce..e9192dae66a46 100644
--- a/pandas/tests/apply/test_str.py
+++ b/pandas/tests/apply/test_str.py
@@ -270,7 +270,7 @@ def test_transform_groupby_kernel_series(request, string_series, op):
@pytest.mark.parametrize("op", frame_transform_kernels)
-def test_transform_groupby_kernel_frame(request, axis, float_frame, op):
+def test_transform_groupby_kernel_frame(request, float_frame, op):
if op == "ngroup":
request.applymarker(
pytest.mark.xfail(raises=ValueError, reason="ngroup not valid for NDFrame")
@@ -279,22 +279,15 @@ def test_transform_groupby_kernel_frame(request, axis, float_frame, op):
# GH 35964
args = [0.0] if op == "fillna" else []
- if axis in (0, "index"):
- ones = np.ones(float_frame.shape[0])
- msg = "The 'axis' keyword in DataFrame.groupby is deprecated"
- else:
- ones = np.ones(float_frame.shape[1])
- msg = "DataFrame.groupby with axis=1 is deprecated"
-
- with tm.assert_produces_warning(FutureWarning, match=msg):
- gb = float_frame.groupby(ones, axis=axis)
+ ones = np.ones(float_frame.shape[0])
+ gb = float_frame.groupby(ones)
warn = FutureWarning if op == "fillna" else None
op_msg = "DataFrameGroupBy.fillna is deprecated"
with tm.assert_produces_warning(warn, match=op_msg):
expected = gb.transform(op, *args)
- result = float_frame.transform(op, axis, *args)
+ result = float_frame.transform(op, 0, *args)
tm.assert_frame_equal(result, expected)
# same thing, but ensuring we have multiple blocks
@@ -302,17 +295,10 @@ def test_transform_groupby_kernel_frame(request, axis, float_frame, op):
float_frame["E"] = float_frame["A"].copy()
assert len(float_frame._mgr.arrays) > 1
- if axis in (0, "index"):
- ones = np.ones(float_frame.shape[0])
- else:
- ones = np.ones(float_frame.shape[1])
- with tm.assert_produces_warning(FutureWarning, match=msg):
- gb2 = float_frame.groupby(ones, axis=axis)
- warn = FutureWarning if op == "fillna" else None
- op_msg = "DataFrameGroupBy.fillna is deprecated"
- with tm.assert_produces_warning(warn, match=op_msg):
- expected2 = gb2.transform(op, *args)
- result2 = float_frame.transform(op, axis, *args)
+ ones = np.ones(float_frame.shape[0])
+ gb2 = float_frame.groupby(ones)
+ expected2 = gb2.transform(op, *args)
+ result2 = float_frame.transform(op, 0, *args)
tm.assert_frame_equal(result2, expected2)
diff --git a/pandas/tests/groupby/aggregate/test_aggregate.py b/pandas/tests/groupby/aggregate/test_aggregate.py
index 0e86f95a93091..c6962815ffda1 100644
--- a/pandas/tests/groupby/aggregate/test_aggregate.py
+++ b/pandas/tests/groupby/aggregate/test_aggregate.py
@@ -111,28 +111,6 @@ def test_groupby_aggregation_mixed_dtype():
tm.assert_frame_equal(result, expected)
-def test_groupby_aggregation_multi_level_column():
- # GH 29772
- lst = [
- [True, True, True, False],
- [True, False, np.nan, False],
- [True, True, np.nan, False],
- [True, True, np.nan, False],
- ]
- df = DataFrame(
- data=lst,
- columns=MultiIndex.from_tuples([("A", 0), ("A", 1), ("B", 0), ("B", 1)]),
- )
-
- msg = "DataFrame.groupby with axis=1 is deprecated"
- with tm.assert_produces_warning(FutureWarning, match=msg):
- gb = df.groupby(level=1, axis=1)
- result = gb.sum(numeric_only=False)
- expected = DataFrame({0: [2.0, True, True, True], 1: [1, 0, 1, 1]})
-
- tm.assert_frame_equal(result, expected)
-
-
def test_agg_apply_corner(ts, tsframe):
# nothing to group, all NA
grouped = ts.groupby(ts * np.nan, group_keys=False)
@@ -268,65 +246,6 @@ def test_agg_str_with_kwarg_axis_1_raises(df, reduction_func):
gb.agg(reduction_func, axis=1)
-@pytest.mark.parametrize(
- "func, expected, dtype, result_dtype_dict",
- [
- ("sum", [5, 7, 9], "int64", {}),
- ("std", [4.5**0.5] * 3, int, {"i": float, "j": float, "k": float}),
- ("var", [4.5] * 3, int, {"i": float, "j": float, "k": float}),
- ("sum", [5, 7, 9], "Int64", {"j": "int64"}),
- ("std", [4.5**0.5] * 3, "Int64", {"i": float, "j": float, "k": float}),
- ("var", [4.5] * 3, "Int64", {"i": "float64", "j": "float64", "k": "float64"}),
- ],
-)
-def test_multiindex_groupby_mixed_cols_axis1(func, expected, dtype, result_dtype_dict):
- # GH#43209
- df = DataFrame(
- [[1, 2, 3, 4, 5, 6]] * 3,
- columns=MultiIndex.from_product([["a", "b"], ["i", "j", "k"]]),
- ).astype({("a", "j"): dtype, ("b", "j"): dtype})
-
- msg = "DataFrame.groupby with axis=1 is deprecated"
- with tm.assert_produces_warning(FutureWarning, match=msg):
- gb = df.groupby(level=1, axis=1)
- result = gb.agg(func)
- expected = DataFrame([expected] * 3, columns=["i", "j", "k"]).astype(
- result_dtype_dict
- )
-
- tm.assert_frame_equal(result, expected)
-
-
-@pytest.mark.parametrize(
- "func, expected_data, result_dtype_dict",
- [
- ("sum", [[2, 4], [10, 12], [18, 20]], {10: "int64", 20: "int64"}),
- # std should ideally return Int64 / Float64 #43330
- ("std", [[2**0.5] * 2] * 3, "float64"),
- ("var", [[2] * 2] * 3, {10: "float64", 20: "float64"}),
- ],
-)
-def test_groupby_mixed_cols_axis1(func, expected_data, result_dtype_dict):
- # GH#43209
- df = DataFrame(
- np.arange(12).reshape(3, 4),
- index=Index([0, 1, 0], name="y"),
- columns=Index([10, 20, 10, 20], name="x"),
- dtype="int64",
- ).astype({10: "Int64"})
-
- msg = "DataFrame.groupby with axis=1 is deprecated"
- with tm.assert_produces_warning(FutureWarning, match=msg):
- gb = df.groupby("x", axis=1)
- result = gb.agg(func)
- expected = DataFrame(
- data=expected_data,
- index=Index([0, 1, 0], name="y"),
- columns=Index([10, 20], name="x"),
- ).astype(result_dtype_dict)
- tm.assert_frame_equal(result, expected)
-
-
def test_aggregate_item_by_item(df):
grouped = df.groupby("A")
@@ -1616,19 +1535,6 @@ def test_groupby_complex_raises(func):
data.groupby(data.index % 2).agg(func)
-@pytest.mark.parametrize(
- "func", [["min"], ["mean", "max"], {"b": "sum"}, {"b": "prod", "c": "median"}]
-)
-def test_multi_axis_1_raises(func):
- # GH#46995
- df = DataFrame({"a": [1, 1, 2], "b": [3, 4, 5], "c": [6, 7, 8]})
- msg = "DataFrame.groupby with axis=1 is deprecated"
- with tm.assert_produces_warning(FutureWarning, match=msg):
- gb = df.groupby("a", axis=1)
- with pytest.raises(NotImplementedError, match="axis other than 0 is not supported"):
- gb.agg(func)
-
-
@pytest.mark.parametrize(
"test, constant",
[
diff --git a/pandas/tests/groupby/aggregate/test_cython.py b/pandas/tests/groupby/aggregate/test_cython.py
index 5c99882cef6d2..b1f8ecc9c8a39 100644
--- a/pandas/tests/groupby/aggregate/test_cython.py
+++ b/pandas/tests/groupby/aggregate/test_cython.py
@@ -126,21 +126,6 @@ def test_cython_agg_nothing_to_agg_with_dates():
frame.groupby("b").dates.mean(numeric_only=True)
-def test_cython_agg_frame_columns():
- # #2113
- df = DataFrame({"x": [1, 2, 3], "y": [3, 4, 5]})
-
- msg = "DataFrame.groupby with axis=1 is deprecated"
- with tm.assert_produces_warning(FutureWarning, match=msg):
- df.groupby(level=0, axis="columns").mean()
- with tm.assert_produces_warning(FutureWarning, match=msg):
- df.groupby(level=0, axis="columns").mean()
- with tm.assert_produces_warning(FutureWarning, match=msg):
- df.groupby(level=0, axis="columns").mean()
- with tm.assert_produces_warning(FutureWarning, match=msg):
- df.groupby(level=0, axis="columns").mean()
-
-
def test_cython_agg_return_dict():
# GH 16741
df = DataFrame(
diff --git a/pandas/tests/groupby/methods/test_describe.py b/pandas/tests/groupby/methods/test_describe.py
index e73fb15a54181..274e23abd77d9 100644
--- a/pandas/tests/groupby/methods/test_describe.py
+++ b/pandas/tests/groupby/methods/test_describe.py
@@ -87,18 +87,6 @@ def test_frame_describe_multikey(tsframe):
expected = pd.concat(desc_groups, axis=1)
tm.assert_frame_equal(result, expected)
- msg = "DataFrame.groupby with axis=1 is deprecated"
- with tm.assert_produces_warning(FutureWarning, match=msg):
- groupedT = tsframe.groupby({"A": 0, "B": 0, "C": 1, "D": 1}, axis=1)
- result = groupedT.describe()
- expected = tsframe.describe().T
- # reverting the change from https://github.com/pandas-dev/pandas/pull/35441/
- expected.index = MultiIndex(
- levels=[[0, 1], expected.index],
- codes=[[0, 0, 1, 1], range(len(expected.index))],
- )
- tm.assert_frame_equal(result, expected)
-
def test_frame_describe_tupleindex():
# GH 14848 - regression from 0.19.0 to 0.19.1
diff --git a/pandas/tests/groupby/methods/test_nth.py b/pandas/tests/groupby/methods/test_nth.py
index 52d63cb720485..1b852abad6c8e 100644
--- a/pandas/tests/groupby/methods/test_nth.py
+++ b/pandas/tests/groupby/methods/test_nth.py
@@ -540,32 +540,6 @@ def test_groupby_head_tail(op, n, expected_rows, columns, as_index):
tm.assert_frame_equal(result, expected)
-@pytest.mark.parametrize(
- "op, n, expected_cols",
- [
- ("head", -1, [0]),
- ("head", 0, []),
- ("head", 1, [0, 2]),
- ("head", 7, [0, 1, 2]),
- ("tail", -1, [1]),
- ("tail", 0, []),
- ("tail", 1, [1, 2]),
- ("tail", 7, [0, 1, 2]),
- ],
-)
-def test_groupby_head_tail_axis_1(op, n, expected_cols):
- # GH 9772
- df = DataFrame(
- [[1, 2, 3], [1, 4, 5], [2, 6, 7], [3, 8, 9]], columns=["A", "B", "C"]
- )
- msg = "DataFrame.groupby with axis=1 is deprecated"
- with tm.assert_produces_warning(FutureWarning, match=msg):
- g = df.groupby([0, 0, 1], axis=1)
- expected = df.iloc[:, expected_cols]
- result = getattr(g, op)(n)
- tm.assert_frame_equal(result, expected)
-
-
def test_group_selection_cache():
# GH 12839 nth, head, and tail should return same result consistently
df = DataFrame([[1, 2], [1, 4], [5, 6]], columns=["A", "B"])
@@ -773,24 +747,6 @@ def test_np_ints(slice_test_df, slice_test_grouped):
tm.assert_frame_equal(result, expected)
-def test_groupby_nth_with_column_axis():
- # GH43926
- df = DataFrame(
- [
- [4, 5, 6],
- [8, 8, 7],
- ],
- index=["z", "y"],
- columns=["C", "B", "A"],
- )
- msg = "DataFrame.groupby with axis=1 is deprecated"
- with tm.assert_produces_warning(FutureWarning, match=msg):
- gb = df.groupby(df.iloc[1], axis=1)
- result = gb.nth(0)
- expected = df.iloc[:, [0, 2]]
- tm.assert_frame_equal(result, expected)
-
-
def test_groupby_nth_interval():
# GH#24205
idx_result = MultiIndex(
@@ -814,35 +770,6 @@ def test_groupby_nth_interval():
tm.assert_frame_equal(result, expected)
-@pytest.mark.parametrize(
- "start, stop, expected_values, expected_columns",
- [
- (None, None, [0, 1, 2, 3, 4], list("ABCDE")),
- (None, 1, [0, 3], list("AD")),
- (None, 9, [0, 1, 2, 3, 4], list("ABCDE")),
- (None, -1, [0, 1, 3], list("ABD")),
- (1, None, [1, 2, 4], list("BCE")),
- (1, -1, [1], list("B")),
- (-1, None, [2, 4], list("CE")),
- (-1, 2, [4], list("E")),
- ],
-)
-@pytest.mark.parametrize("method", ["call", "index"])
-def test_nth_slices_with_column_axis(
- start, stop, expected_values, expected_columns, method
-):
- df = DataFrame([range(5)], columns=[list("ABCDE")])
- msg = "DataFrame.groupby with axis=1 is deprecated"
- with tm.assert_produces_warning(FutureWarning, match=msg):
- gb = df.groupby([5, 5, 5, 6, 6], axis=1)
- result = {
- "call": lambda start, stop: gb.nth(slice(start, stop)),
- "index": lambda start, stop: gb.nth[start:stop],
- }[method](start, stop)
- expected = DataFrame([expected_values], columns=[expected_columns])
- tm.assert_frame_equal(result, expected)
-
-
@pytest.mark.filterwarnings(
"ignore:invalid value encountered in remainder:RuntimeWarning"
)
diff --git a/pandas/tests/groupby/methods/test_quantile.py b/pandas/tests/groupby/methods/test_quantile.py
index 1d2e639314cba..af0deba138469 100644
--- a/pandas/tests/groupby/methods/test_quantile.py
+++ b/pandas/tests/groupby/methods/test_quantile.py
@@ -377,32 +377,6 @@ def test_groupby_timedelta_quantile():
tm.assert_frame_equal(result, expected)
-def test_columns_groupby_quantile():
- # GH 33795
- df = DataFrame(
- np.arange(12).reshape(3, -1),
- index=list("XYZ"),
- columns=pd.Series(list("ABAB"), name="col"),
- )
- msg = "DataFrame.groupby with axis=1 is deprecated"
- with tm.assert_produces_warning(FutureWarning, match=msg):
- gb = df.groupby("col", axis=1)
- result = gb.quantile(q=[0.8, 0.2])
- expected = DataFrame(
- [
- [1.6, 0.4, 2.6, 1.4],
- [5.6, 4.4, 6.6, 5.4],
- [9.6, 8.4, 10.6, 9.4],
- ],
- index=list("XYZ"),
- columns=pd.MultiIndex.from_tuples(
- [("A", 0.8), ("A", 0.2), ("B", 0.8), ("B", 0.2)], names=["col", None]
- ),
- )
-
- tm.assert_frame_equal(result, expected)
-
-
def test_timestamp_groupby_quantile(unit):
# GH 33168
dti = pd.date_range(
diff --git a/pandas/tests/groupby/methods/test_size.py b/pandas/tests/groupby/methods/test_size.py
index fd55ceedd1083..5a3eb49e97fb7 100644
--- a/pandas/tests/groupby/methods/test_size.py
+++ b/pandas/tests/groupby/methods/test_size.py
@@ -3,8 +3,6 @@
import pandas.util._test_decorators as td
-from pandas.core.dtypes.common import is_integer_dtype
-
from pandas import (
DataFrame,
Index,
@@ -22,35 +20,6 @@ def test_size(df, by):
assert result[key] == len(group)
-@pytest.mark.parametrize(
- "by",
- [
- [0, 0, 0, 0],
- [0, 1, 1, 1],
- [1, 0, 1, 1],
- [0, None, None, None],
- pytest.param([None, None, None, None], marks=pytest.mark.xfail),
- ],
-)
-@pytest.mark.parametrize("axis_1", [1, "columns"])
-def test_size_axis_1(df, axis_1, by, sort, dropna):
- # GH#45715
- counts = {key: sum(value == key for value in by) for key in dict.fromkeys(by)}
- if dropna:
- counts = {key: value for key, value in counts.items() if key is not None}
- expected = Series(counts, dtype="int64")
- if sort:
- expected = expected.sort_index()
- if is_integer_dtype(expected.index.dtype) and not any(x is None for x in by):
- expected.index = expected.index.astype(int)
-
- msg = "DataFrame.groupby with axis=1 is deprecated"
- with tm.assert_produces_warning(FutureWarning, match=msg):
- grouped = df.groupby(by=by, axis=axis_1, sort=sort, dropna=dropna)
- result = grouped.size()
- tm.assert_series_equal(result, expected)
-
-
@pytest.mark.parametrize("by", ["A", "B", ["A", "B"]])
def test_size_sort(sort, by):
df = DataFrame(np.random.default_rng(2).choice(20, (1000, 3)), columns=list("ABC"))
diff --git a/pandas/tests/groupby/methods/test_value_counts.py b/pandas/tests/groupby/methods/test_value_counts.py
index 42f949443e33f..4d610018917f6 100644
--- a/pandas/tests/groupby/methods/test_value_counts.py
+++ b/pandas/tests/groupby/methods/test_value_counts.py
@@ -232,14 +232,6 @@ def education_df():
)
-def test_axis(education_df):
- msg = "DataFrame.groupby with axis=1 is deprecated"
- with tm.assert_produces_warning(FutureWarning, match=msg):
- gp = education_df.groupby("country", axis=1)
- with pytest.raises(NotImplementedError, match="axis"):
- gp.value_counts()
-
-
def test_bad_subset(education_df):
gp = education_df.groupby("country")
with pytest.raises(ValueError, match="subset"):
diff --git a/pandas/tests/groupby/test_apply.py b/pandas/tests/groupby/test_apply.py
index 29070e686d91a..26b31e202e6e6 100644
--- a/pandas/tests/groupby/test_apply.py
+++ b/pandas/tests/groupby/test_apply.py
@@ -122,40 +122,6 @@ def test_apply_index_date_object(using_infer_string):
tm.assert_series_equal(result, expected)
-def test_apply_trivial(using_infer_string):
- # GH 20066
- # trivial apply: ignore input and return a constant dataframe.
- df = DataFrame(
- {"key": ["a", "a", "b", "b", "a"], "data": [1.0, 2.0, 3.0, 4.0, 5.0]},
- columns=["key", "data"],
- )
- dtype = "string" if using_infer_string else "object"
- expected = pd.concat([df.iloc[1:], df.iloc[1:]], axis=1, keys=["float64", dtype])
-
- msg = "DataFrame.groupby with axis=1 is deprecated"
- with tm.assert_produces_warning(FutureWarning, match=msg):
- gb = df.groupby([str(x) for x in df.dtypes], axis=1)
- result = gb.apply(lambda x: df.iloc[1:])
-
- tm.assert_frame_equal(result, expected)
-
-
-def test_apply_trivial_fail(using_infer_string):
- # GH 20066
- df = DataFrame(
- {"key": ["a", "a", "b", "b", "a"], "data": [1.0, 2.0, 3.0, 4.0, 5.0]},
- columns=["key", "data"],
- )
- dtype = "string" if using_infer_string else "object"
- expected = pd.concat([df, df], axis=1, keys=["float64", dtype])
- msg = "DataFrame.groupby with axis=1 is deprecated"
- with tm.assert_produces_warning(FutureWarning, match=msg):
- gb = df.groupby([str(x) for x in df.dtypes], axis=1, group_keys=True)
- result = gb.apply(lambda x: df)
-
- tm.assert_frame_equal(result, expected)
-
-
@pytest.mark.parametrize(
"df, group_names",
[
@@ -1257,31 +1223,6 @@ def test_apply_with_date_in_multiindex_does_not_convert_to_timestamp():
assert type(val) is date
-def test_apply_by_cols_equals_apply_by_rows_transposed():
- # GH 16646
- # Operating on the columns, or transposing and operating on the rows
- # should give the same result. There was previously a bug where the
- # by_rows operation would work fine, but by_cols would throw a ValueError
-
- df = DataFrame(
- np.random.default_rng(2).random([6, 4]),
- columns=MultiIndex.from_product([["A", "B"], [1, 2]]),
- )
-
- msg = "The 'axis' keyword in DataFrame.groupby is deprecated"
- with tm.assert_produces_warning(FutureWarning, match=msg):
- gb = df.T.groupby(axis=0, level=0)
- by_rows = gb.apply(lambda x: x.droplevel(axis=0, level=0))
-
- msg = "DataFrame.groupby with axis=1 is deprecated"
- with tm.assert_produces_warning(FutureWarning, match=msg):
- gb2 = df.groupby(axis=1, level=0)
- by_cols = gb2.apply(lambda x: x.droplevel(axis=1, level=0))
-
- tm.assert_frame_equal(by_cols, by_rows.T)
- tm.assert_frame_equal(by_cols, df)
-
-
def test_apply_dropna_with_indexed_same(dropna):
# GH 38227
# GH#43205
diff --git a/pandas/tests/groupby/test_apply_mutate.py b/pandas/tests/groupby/test_apply_mutate.py
index cfd1a4bca9d91..29d82cce44807 100644
--- a/pandas/tests/groupby/test_apply_mutate.py
+++ b/pandas/tests/groupby/test_apply_mutate.py
@@ -98,66 +98,3 @@ def fn(x):
name="col2",
)
tm.assert_series_equal(result, expected)
-
-
-def test_apply_mutate_columns_multiindex():
- # GH 12652
- df = pd.DataFrame(
- {
- ("C", "julian"): [1, 2, 3],
- ("B", "geoffrey"): [1, 2, 3],
- ("A", "julian"): [1, 2, 3],
- ("B", "julian"): [1, 2, 3],
- ("A", "geoffrey"): [1, 2, 3],
- ("C", "geoffrey"): [1, 2, 3],
- },
- columns=pd.MultiIndex.from_tuples(
- [
- ("A", "julian"),
- ("A", "geoffrey"),
- ("B", "julian"),
- ("B", "geoffrey"),
- ("C", "julian"),
- ("C", "geoffrey"),
- ]
- ),
- )
-
- def add_column(grouped):
- name = grouped.columns[0][1]
- grouped["sum", name] = grouped.sum(axis=1)
- return grouped
-
- msg = "DataFrame.groupby with axis=1 is deprecated"
- with tm.assert_produces_warning(FutureWarning, match=msg):
- gb = df.groupby(level=1, axis=1)
- result = gb.apply(add_column)
- expected = pd.DataFrame(
- [
- [1, 1, 1, 3, 1, 1, 1, 3],
- [2, 2, 2, 6, 2, 2, 2, 6],
- [
- 3,
- 3,
- 3,
- 9,
- 3,
- 3,
- 3,
- 9,
- ],
- ],
- columns=pd.MultiIndex.from_tuples(
- [
- ("geoffrey", "A", "geoffrey"),
- ("geoffrey", "B", "geoffrey"),
- ("geoffrey", "C", "geoffrey"),
- ("geoffrey", "sum", "geoffrey"),
- ("julian", "A", "julian"),
- ("julian", "B", "julian"),
- ("julian", "C", "julian"),
- ("julian", "sum", "julian"),
- ]
- ),
- )
- tm.assert_frame_equal(result, expected)
diff --git a/pandas/tests/groupby/test_categorical.py b/pandas/tests/groupby/test_categorical.py
index 7db08c8879b0c..727a77f52fe48 100644
--- a/pandas/tests/groupby/test_categorical.py
+++ b/pandas/tests/groupby/test_categorical.py
@@ -1348,22 +1348,6 @@ def test_groupby_categorical_series_dataframe_consistent(df_cat):
tm.assert_series_equal(result, expected)
-@pytest.mark.parametrize("code", [([1, 0, 0]), ([0, 0, 0])])
-def test_groupby_categorical_axis_1(code):
- # GH 13420
- df = DataFrame({"a": [1, 2, 3, 4], "b": [-1, -2, -3, -4], "c": [5, 6, 7, 8]})
- cat = Categorical.from_codes(code, categories=list("abc"))
- msg = "DataFrame.groupby with axis=1 is deprecated"
- with tm.assert_produces_warning(FutureWarning, match=msg):
- gb = df.groupby(cat, axis=1, observed=False)
- result = gb.mean()
- msg = "The 'axis' keyword in DataFrame.groupby is deprecated"
- with tm.assert_produces_warning(FutureWarning, match=msg):
- gb2 = df.T.groupby(cat, axis=0, observed=False)
- expected = gb2.mean().T
- tm.assert_frame_equal(result, expected)
-
-
def test_groupby_cat_preserves_structure(observed, ordered):
# GH 28787
df = DataFrame(
diff --git a/pandas/tests/groupby/test_filters.py b/pandas/tests/groupby/test_filters.py
index 309c4b7b57e84..a34170e9b55db 100644
--- a/pandas/tests/groupby/test_filters.py
+++ b/pandas/tests/groupby/test_filters.py
@@ -121,19 +121,6 @@ def raise_if_sum_is_zero(x):
grouped.filter(raise_if_sum_is_zero)
-def test_filter_with_axis_in_groupby():
- # issue 11041
- index = pd.MultiIndex.from_product([range(10), [0, 1]])
- data = DataFrame(np.arange(100).reshape(-1, 20), columns=index, dtype="int64")
-
- msg = "DataFrame.groupby with axis=1"
- with tm.assert_produces_warning(FutureWarning, match=msg):
- gb = data.groupby(level=0, axis=1)
- result = gb.filter(lambda x: x.iloc[0, 0] > 10)
- expected = data.iloc[:, 12:20]
- tm.assert_frame_equal(result, expected)
-
-
def test_filter_bad_shapes():
df = DataFrame({"A": np.arange(8), "B": list("aabbbbcc"), "C": np.arange(8)})
s = df["B"]
diff --git a/pandas/tests/groupby/test_groupby.py b/pandas/tests/groupby/test_groupby.py
index 399cebb0d3706..a06d104e7e44c 100644
--- a/pandas/tests/groupby/test_groupby.py
+++ b/pandas/tests/groupby/test_groupby.py
@@ -39,7 +39,7 @@
def test_repr():
# GH18203
result = repr(Grouper(key="A", level="B"))
- expected = "Grouper(key='A', level='B', axis=0, sort=False, dropna=True)"
+ expected = "Grouper(key='A', level='B', sort=False, dropna=True)"
assert result == expected
@@ -288,29 +288,6 @@ def test_frame_groupby(tsframe):
assert (samething == v).all()
-def test_frame_groupby_columns(tsframe):
- mapping = {"A": 0, "B": 0, "C": 1, "D": 1}
- msg = "DataFrame.groupby with axis=1 is deprecated"
- with tm.assert_produces_warning(FutureWarning, match=msg):
- grouped = tsframe.groupby(mapping, axis=1)
-
- # aggregate
- aggregated = grouped.aggregate("mean")
- assert len(aggregated) == len(tsframe)
- assert len(aggregated.columns) == 2
-
- # transform
- tf = lambda x: x - x.mean()
- msg = "The 'axis' keyword in DataFrame.groupby is deprecated"
- with tm.assert_produces_warning(FutureWarning, match=msg):
- groupedT = tsframe.T.groupby(mapping, axis=0)
- tm.assert_frame_equal(groupedT.transform(tf).T, grouped.transform(tf))
-
- # iterate
- for k, v in grouped:
- assert len(v.columns) == 2
-
-
def test_frame_set_name_single(df):
grouped = df.groupby("A")
@@ -638,18 +615,6 @@ def test_groupby_as_index_series_scalar(df):
tm.assert_frame_equal(result, expected)
-def test_groupby_as_index_corner(df, ts):
- msg = "as_index=False only valid with DataFrame"
- with pytest.raises(TypeError, match=msg):
- ts.groupby(lambda x: x.weekday(), as_index=False)
-
- msg = "as_index=False only valid for axis=0"
- depr_msg = "DataFrame.groupby with axis=1 is deprecated"
- with pytest.raises(ValueError, match=msg):
- with tm.assert_produces_warning(FutureWarning, match=depr_msg):
- df.groupby(lambda x: x.lower(), as_index=False, axis=1)
-
-
def test_groupby_multiple_key():
df = DataFrame(
np.random.default_rng(2).standard_normal((10, 4)),
@@ -660,19 +625,6 @@ def test_groupby_multiple_key():
agged = grouped.sum()
tm.assert_almost_equal(df.values, agged.values)
- depr_msg = "DataFrame.groupby with axis=1 is deprecated"
- with tm.assert_produces_warning(FutureWarning, match=depr_msg):
- grouped = df.T.groupby(
- [lambda x: x.year, lambda x: x.month, lambda x: x.day], axis=1
- )
-
- agged = grouped.agg(lambda x: x.sum())
- tm.assert_index_equal(agged.index, df.columns)
- tm.assert_almost_equal(df.T.values, agged.values)
-
- agged = grouped.agg(lambda x: x.sum())
- tm.assert_almost_equal(df.T.values, agged.values)
-
def test_groupby_multi_corner(df):
# test that having an all-NA column doesn't mess you up
@@ -703,14 +655,6 @@ def test_raises_on_nuisance(df):
with pytest.raises(TypeError, match=msg):
grouped.sum()
- # won't work with axis = 1
- depr_msg = "DataFrame.groupby with axis=1 is deprecated"
- with tm.assert_produces_warning(FutureWarning, match=depr_msg):
- grouped = df.groupby({"A": 0, "C": 0, "D": 1, "E": 1}, axis=1)
- msg = "does not support reduction 'sum'"
- with pytest.raises(TypeError, match=msg):
- grouped.agg(lambda x: x.sum(0, numeric_only=False))
-
@pytest.mark.parametrize(
"agg_function",
@@ -978,24 +922,12 @@ def test_groupby_with_hier_columns():
result = df.groupby(level=0).mean()
tm.assert_index_equal(result.columns, columns)
- depr_msg = "DataFrame.groupby with axis=1 is deprecated"
- with tm.assert_produces_warning(FutureWarning, match=depr_msg):
- gb = df.groupby(level=0, axis=1)
- result = gb.mean()
- tm.assert_index_equal(result.index, df.index)
-
result = df.groupby(level=0).agg("mean")
tm.assert_index_equal(result.columns, columns)
result = df.groupby(level=0).apply(lambda x: x.mean())
tm.assert_index_equal(result.columns, columns)
- with tm.assert_produces_warning(FutureWarning, match=depr_msg):
- gb = df.groupby(level=0, axis=1)
- result = gb.agg(lambda x: x.mean(1))
- tm.assert_index_equal(result.columns, Index(["A", "B"]))
- tm.assert_index_equal(result.index, df.index)
-
# add a nuisance column
sorted_columns, _ = columns.sortlevel(0)
df["A", "foo"] = "bar"
@@ -1997,34 +1929,6 @@ def test_groupby_groups_in_BaseGrouper():
assert result.groups == expected.groups
-@pytest.mark.parametrize("group_name", ["x", ["x"]])
-def test_groupby_axis_1(group_name):
- # GH 27614
- df = DataFrame(
- np.arange(12).reshape(3, 4), index=[0, 1, 0], columns=[10, 20, 10, 20]
- )
- df.index.name = "y"
- df.columns.name = "x"
-
- depr_msg = "DataFrame.groupby with axis=1 is deprecated"
- with tm.assert_produces_warning(FutureWarning, match=depr_msg):
- gb = df.groupby(group_name, axis=1)
-
- results = gb.sum()
- expected = df.T.groupby(group_name).sum().T
- tm.assert_frame_equal(results, expected)
-
- # test on MI column
- iterables = [["bar", "baz", "foo"], ["one", "two"]]
- mi = MultiIndex.from_product(iterables=iterables, names=["x", "x1"])
- df = DataFrame(np.arange(18).reshape(3, 6), index=[0, 1, 0], columns=mi)
- with tm.assert_produces_warning(FutureWarning, match=depr_msg):
- gb = df.groupby(group_name, axis=1)
- results = gb.sum()
- expected = df.T.groupby(group_name).sum().T
- tm.assert_frame_equal(results, expected)
-
-
@pytest.mark.parametrize(
"op, expected",
[
@@ -2156,42 +2060,26 @@ def test_group_on_empty_multiindex(transformation_func, request):
tm.assert_equal(result, expected)
-def test_groupby_crash_on_nunique(axis):
+def test_groupby_crash_on_nunique():
# Fix following 30253
dti = date_range("2016-01-01", periods=2, name="foo")
df = DataFrame({("A", "B"): [1, 2], ("A", "C"): [1, 3], ("D", "B"): [0, 0]})
df.columns.names = ("bar", "baz")
df.index = dti
- axis_number = df._get_axis_number(axis)
- if not axis_number:
- df = df.T
- msg = "The 'axis' keyword in DataFrame.groupby is deprecated"
- else:
- msg = "DataFrame.groupby with axis=1 is deprecated"
-
- with tm.assert_produces_warning(FutureWarning, match=msg):
- gb = df.groupby(axis=axis_number, level=0)
+ df = df.T
+ gb = df.groupby(level=0)
result = gb.nunique()
expected = DataFrame({"A": [1, 2], "D": [1, 1]}, index=dti)
expected.columns.name = "bar"
- if not axis_number:
- expected = expected.T
+ expected = expected.T
tm.assert_frame_equal(result, expected)
- if axis_number == 0:
- # same thing, but empty columns
- with tm.assert_produces_warning(FutureWarning, match=msg):
- gb2 = df[[]].groupby(axis=axis_number, level=0)
- exp = expected[[]]
- else:
- # same thing, but empty rows
- with tm.assert_produces_warning(FutureWarning, match=msg):
- gb2 = df.loc[[]].groupby(axis=axis_number, level=0)
- # default for empty when we can't infer a dtype is float64
- exp = expected.loc[[]].astype(np.float64)
+ # same thing, but empty columns
+ gb2 = df[[]].groupby(level=0)
+ exp = expected[[]]
res = gb2.nunique()
tm.assert_frame_equal(res, exp)
@@ -2267,17 +2155,6 @@ def test_subsetting_columns_keeps_attrs(klass, attr, value):
assert getattr(result, attr) == getattr(expected, attr)
-def test_subsetting_columns_axis_1():
- # GH 37725
- df = DataFrame({"A": [1], "B": [2], "C": [3]})
- msg = "DataFrame.groupby with axis=1 is deprecated"
- with tm.assert_produces_warning(FutureWarning, match=msg):
- g = df.groupby([0, 0, 1], axis=1)
- match = "Cannot subset columns when using axis=1"
- with pytest.raises(ValueError, match=match):
- g[["A", "B"]].sum()
-
-
@pytest.mark.parametrize("func", ["sum", "any", "shift"])
def test_groupby_column_index_name_lost(func):
# GH: 29764 groupby loses index sometimes
@@ -2992,29 +2869,6 @@ def test_groupby_ngroup_with_nan():
tm.assert_series_equal(result, expected)
-def test_get_group_axis_1():
- # GH#54858
- df = DataFrame(
- {
- "col1": [0, 3, 2, 3],
- "col2": [4, 1, 6, 7],
- "col3": [3, 8, 2, 10],
- "col4": [1, 13, 6, 15],
- "col5": [-4, 5, 6, -7],
- }
- )
- with tm.assert_produces_warning(FutureWarning, match="deprecated"):
- grouped = df.groupby(axis=1, by=[1, 2, 3, 2, 1])
- result = grouped.get_group(1)
- expected = DataFrame(
- {
- "col1": [0, 3, 2, 3],
- "col5": [-4, 5, 6, -7],
- }
- )
- tm.assert_frame_equal(result, expected)
-
-
def test_groupby_ffill_with_duplicated_index():
# GH#43412
df = DataFrame({"a": [1, 2, 3, 4, np.nan, np.nan]}, index=[0, 1, 2, 0, 1, 2])
diff --git a/pandas/tests/groupby/test_grouping.py b/pandas/tests/groupby/test_grouping.py
index 39d1ba207fba7..841dd29edab10 100644
--- a/pandas/tests/groupby/test_grouping.py
+++ b/pandas/tests/groupby/test_grouping.py
@@ -230,13 +230,6 @@ def test_grouper_creation_bug(self):
result = g.sum()
tm.assert_frame_equal(result, expected)
- msg = "Grouper axis keyword is deprecated and will be removed"
- with tm.assert_produces_warning(FutureWarning, match=msg):
- gpr = Grouper(key="A", axis=0)
- g = df.groupby(gpr)
- result = g.sum()
- tm.assert_frame_equal(result, expected)
-
msg = "DataFrameGroupBy.apply operated on the grouping columns"
with tm.assert_produces_warning(DeprecationWarning, match=msg):
result = g.apply(lambda x: x.sum())
@@ -386,22 +379,14 @@ def test_groupby_categorical_index_and_columns(self, observed):
[[1, 2, 1, 2], [1, 2, 1, 2], [1, 2, 1, 2], [1, 2, 1, 2], [1, 2, 1, 2]], int
)
cat_columns = CategoricalIndex(columns, categories=categories, ordered=True)
- df = DataFrame(data=data, columns=cat_columns)
- depr_msg = "DataFrame.groupby with axis=1 is deprecated"
- with tm.assert_produces_warning(FutureWarning, match=depr_msg):
- result = df.groupby(axis=1, level=0, observed=observed).sum()
expected_data = np.array([[4, 2], [4, 2], [4, 2], [4, 2], [4, 2]], int)
expected_columns = CategoricalIndex(
categories, categories=categories, ordered=True
)
- expected = DataFrame(data=expected_data, columns=expected_columns)
- tm.assert_frame_equal(result, expected)
# test transposed version
df = DataFrame(data.T, index=cat_columns)
- msg = "The 'axis' keyword in DataFrame.groupby is deprecated"
- with tm.assert_produces_warning(FutureWarning, match=msg):
- result = df.groupby(axis=0, level=0, observed=observed).sum()
+ result = df.groupby(level=0, observed=observed).sum()
expected = DataFrame(data=expected_data.T, index=expected_columns)
tm.assert_frame_equal(result, expected)
@@ -529,18 +514,6 @@ def test_grouping_error_on_multidim_input(self, df):
with pytest.raises(ValueError, match=msg):
Grouping(df.index, df[["A", "A"]])
- def test_multiindex_passthru(self):
- # GH 7997
- # regression from 0.14.1
- df = DataFrame([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
- df.columns = MultiIndex.from_tuples([(0, 1), (1, 1), (2, 1)])
-
- depr_msg = "DataFrame.groupby with axis=1 is deprecated"
- with tm.assert_produces_warning(FutureWarning, match=depr_msg):
- gb = df.groupby(axis=1, level=[0, 1])
- result = gb.first()
- tm.assert_frame_equal(result, df)
-
def test_multiindex_negative_level(self, multiindex_dataframe_random_data):
# GH 13901
result = multiindex_dataframe_random_data.groupby(level=-1).sum()
@@ -677,35 +650,20 @@ def test_groupby_level(self, sort, multiindex_dataframe_random_data, df):
tm.assert_frame_equal(result0, expected0)
tm.assert_frame_equal(result1, expected1)
- # axis=1
- msg = "DataFrame.groupby with axis=1 is deprecated"
- with tm.assert_produces_warning(FutureWarning, match=msg):
- result0 = frame.T.groupby(level=0, axis=1, sort=sort).sum()
- result1 = frame.T.groupby(level=1, axis=1, sort=sort).sum()
- tm.assert_frame_equal(result0, expected0.T)
- tm.assert_frame_equal(result1, expected1.T)
-
# raise exception for non-MultiIndex
msg = "level > 0 or level < -1 only valid with MultiIndex"
with pytest.raises(ValueError, match=msg):
df.groupby(level=1)
- def test_groupby_level_index_names(self, axis):
+ def test_groupby_level_index_names(self):
# GH4014 this used to raise ValueError since 'exp'>1 (in py2)
df = DataFrame({"exp": ["A"] * 3 + ["B"] * 3, "var1": range(6)}).set_index(
"exp"
)
- if axis in (1, "columns"):
- df = df.T
- depr_msg = "DataFrame.groupby with axis=1 is deprecated"
- else:
- depr_msg = "The 'axis' keyword in DataFrame.groupby is deprecated"
- with tm.assert_produces_warning(FutureWarning, match=depr_msg):
- df.groupby(level="exp", axis=axis)
- msg = f"level name foo is not the name of the {df._get_axis_name(axis)}"
+ df.groupby(level="exp")
+ msg = "level name foo is not the name of the index"
with pytest.raises(ValueError, match=msg):
- with tm.assert_produces_warning(FutureWarning, match=depr_msg):
- df.groupby(level="foo", axis=axis)
+ df.groupby(level="foo")
def test_groupby_level_with_nas(self, sort):
# GH 17537
@@ -1099,14 +1057,6 @@ def test_multi_iter_frame(self, three_group):
groups = {key: gp for key, gp in grouped} # noqa: C416
assert len(groups) == 2
- # axis = 1
- three_levels = three_group.groupby(["A", "B", "C"]).mean()
- depr_msg = "DataFrame.groupby with axis=1 is deprecated"
- with tm.assert_produces_warning(FutureWarning, match=depr_msg):
- grouped = three_levels.T.groupby(axis=1, level=(1, 2))
- for key, group in grouped:
- pass
-
def test_dictify(self, df):
dict(iter(df.groupby("A")))
dict(iter(df.groupby(["A", "B"])))
diff --git a/pandas/tests/groupby/test_indexing.py b/pandas/tests/groupby/test_indexing.py
index f839bf156ca00..a3d3f509e186a 100644
--- a/pandas/tests/groupby/test_indexing.py
+++ b/pandas/tests/groupby/test_indexing.py
@@ -266,20 +266,6 @@ def test_step(step):
tm.assert_frame_equal(result, expected)
-def test_column_axis():
- column_group_df = pd.DataFrame(
- [[0, 1, 2, 3, 4, 5, 6], [0, 0, 1, 0, 1, 0, 2]],
- columns=["A", "B", "C", "D", "E", "F", "G"],
- )
- msg = "DataFrame.groupby with axis=1"
- with tm.assert_produces_warning(FutureWarning, match=msg):
- g = column_group_df.groupby(column_group_df.iloc[1], axis=1)
- result = g._positional_selector[1:-1]
- expected = column_group_df.iloc[:, [1, 3]]
-
- tm.assert_frame_equal(result, expected)
-
-
def test_columns_on_iter():
# GitHub issue #44821
df = pd.DataFrame({k: range(10) for k in "ABC"})
diff --git a/pandas/tests/groupby/test_numba.py b/pandas/tests/groupby/test_numba.py
index ee7d342472493..3e32031e51138 100644
--- a/pandas/tests/groupby/test_numba.py
+++ b/pandas/tests/groupby/test_numba.py
@@ -61,13 +61,6 @@ def test_as_index_false_unsupported(self, numba_supported_reductions):
with pytest.raises(NotImplementedError, match="as_index=False"):
getattr(gb, func)(engine="numba", **kwargs)
- def test_axis_1_unsupported(self, numba_supported_reductions):
- func, kwargs = numba_supported_reductions
- df = DataFrame({"a": [3, 2, 3, 2], "b": range(4), "c": range(1, 5)})
- gb = df.groupby("a", axis=1)
- with pytest.raises(NotImplementedError, match="axis=1"):
- getattr(gb, func)(engine="numba", **kwargs)
-
def test_no_engine_doesnt_raise(self):
# GH55520
df = DataFrame({"a": [3, 2, 3, 2], "b": range(4), "c": range(1, 5)})
diff --git a/pandas/tests/groupby/test_reductions.py b/pandas/tests/groupby/test_reductions.py
index 08ce41edfb784..50103011693bc 100644
--- a/pandas/tests/groupby/test_reductions.py
+++ b/pandas/tests/groupby/test_reductions.py
@@ -1149,36 +1149,26 @@ def test_apply_to_nullable_integer_returns_float(values, function):
"sem",
],
)
-@pytest.mark.parametrize("axis", [0, 1])
-def test_regression_allowlist_methods(op, axis, skipna, sort):
+def test_regression_allowlist_methods(op, skipna, sort):
# GH6944
# GH 17537
# explicitly test the allowlist methods
- raw_frame = DataFrame([0])
- if axis == 0:
- frame = raw_frame
- msg = "The 'axis' keyword in DataFrame.groupby is deprecated and will be"
- else:
- frame = raw_frame.T
- msg = "DataFrame.groupby with axis=1 is deprecated"
+ frame = DataFrame([0])
- with tm.assert_produces_warning(FutureWarning, match=msg):
- grouped = frame.groupby(level=0, axis=axis, sort=sort)
+ grouped = frame.groupby(level=0, sort=sort)
if op == "skew":
# skew has skipna
result = getattr(grouped, op)(skipna=skipna)
- expected = frame.groupby(level=0).apply(
- lambda h: getattr(h, op)(axis=axis, skipna=skipna)
- )
+ expected = frame.groupby(level=0).apply(lambda h: getattr(h, op)(skipna=skipna))
if sort:
- expected = expected.sort_index(axis=axis)
+ expected = expected.sort_index()
tm.assert_frame_equal(result, expected)
else:
result = getattr(grouped, op)()
- expected = frame.groupby(level=0).apply(lambda h: getattr(h, op)(axis=axis))
+ expected = frame.groupby(level=0).apply(lambda h: getattr(h, op)())
if sort:
- expected = expected.sort_index(axis=axis)
+ expected = expected.sort_index()
tm.assert_frame_equal(result, expected)
diff --git a/pandas/tests/groupby/test_timegrouper.py b/pandas/tests/groupby/test_timegrouper.py
index 8ef7c2b8ce859..b8891da388695 100644
--- a/pandas/tests/groupby/test_timegrouper.py
+++ b/pandas/tests/groupby/test_timegrouper.py
@@ -925,7 +925,7 @@ def test_groupby_apply_timegrouper_with_nat_apply_squeeze(
# check that we will go through the singular_series path
# in _wrap_applied_output_series
assert gb.ngroups == 1
- assert gb._selected_obj._get_axis(gb.axis).nlevels == 1
+ assert gb._selected_obj.index.nlevels == 1
# function that returns a Series
msg = "DataFrameGroupBy.apply operated on the grouping columns"
diff --git a/pandas/tests/groupby/transform/test_transform.py b/pandas/tests/groupby/transform/test_transform.py
index 3bccacf3dec6f..67bebddaa63ca 100644
--- a/pandas/tests/groupby/transform/test_transform.py
+++ b/pandas/tests/groupby/transform/test_transform.py
@@ -170,69 +170,9 @@ def test_transform_broadcast(tsframe, ts):
for col in tsframe:
assert_fp_equal(res[col], agged[col])
- # group columns
- msg = "DataFrame.groupby with axis=1 is deprecated"
- with tm.assert_produces_warning(FutureWarning, match=msg):
- grouped = tsframe.groupby({"A": 0, "B": 0, "C": 1, "D": 1}, axis=1)
- msg = "using DataFrameGroupBy.mean"
- with tm.assert_produces_warning(FutureWarning, match=msg):
- result = grouped.transform(np.mean)
- tm.assert_index_equal(result.index, tsframe.index)
- tm.assert_index_equal(result.columns, tsframe.columns)
- for _, gp in grouped:
- agged = gp.mean(1)
- res = result.reindex(columns=gp.columns)
- for idx in gp.index:
- assert_fp_equal(res.xs(idx), agged[idx])
-
-
-def test_transform_axis_1(request, transformation_func):
- # GH 36308
-
- df = DataFrame({"a": [1, 2], "b": [3, 4], "c": [5, 6]}, index=["x", "y"])
- args = get_groupby_method_args(transformation_func, df)
- msg = "DataFrame.groupby with axis=1 is deprecated"
- with tm.assert_produces_warning(FutureWarning, match=msg):
- gb = df.groupby([0, 0, 1], axis=1)
- warn = FutureWarning if transformation_func == "fillna" else None
- msg = "DataFrameGroupBy.fillna is deprecated"
- with tm.assert_produces_warning(warn, match=msg):
- result = gb.transform(transformation_func, *args)
- msg = "DataFrameGroupBy.fillna is deprecated"
- with tm.assert_produces_warning(warn, match=msg):
- expected = df.T.groupby([0, 0, 1]).transform(transformation_func, *args).T
-
- if transformation_func in ["diff", "shift"]:
- # Result contains nans, so transpose coerces to float
- expected["b"] = expected["b"].astype("int64")
-
- # cumcount returns Series; the rest are DataFrame
- tm.assert_equal(result, expected)
-
-
-def test_transform_axis_1_reducer(request, reduction_func):
- # GH#45715
- if reduction_func in (
- "corrwith",
- "ngroup",
- "nth",
- ):
- marker = pytest.mark.xfail(reason="transform incorrectly fails - GH#45986")
- request.applymarker(marker)
-
- df = DataFrame({"a": [1, 2], "b": [3, 4], "c": [5, 6]}, index=["x", "y"])
- msg = "DataFrame.groupby with axis=1 is deprecated"
- with tm.assert_produces_warning(FutureWarning, match=msg):
- gb = df.groupby([0, 0, 1], axis=1)
-
- result = gb.transform(reduction_func)
- expected = df.T.groupby([0, 0, 1]).transform(reduction_func).T
- tm.assert_equal(result, expected)
-
def test_transform_axis_ts(tsframe):
- # make sure that we are setting the axes
- # correctly when on axis=0 or 1
+ # make sure that we are setting the axes correctly
# in the presence of a non-monotonic indexer
# GH12713
@@ -252,14 +192,6 @@ def test_transform_axis_ts(tsframe):
expected = grouped.apply(lambda x: x - x.mean(axis=0))
tm.assert_frame_equal(result, expected)
- ts = ts.T
- msg = "DataFrame.groupby with axis=1 is deprecated"
- with tm.assert_produces_warning(FutureWarning, match=msg):
- grouped = ts.groupby(lambda x: x.weekday(), axis=1, group_keys=False)
- result = ts - grouped.transform("mean")
- expected = grouped.apply(lambda x: (x.T - x.mean(1)).T)
- tm.assert_frame_equal(result, expected)
-
# non-monotonic
ts = tso.iloc[[1, 0] + list(range(2, len(base)))]
grouped = ts.groupby(lambda x: x.weekday(), group_keys=False)
@@ -267,14 +199,6 @@ def test_transform_axis_ts(tsframe):
expected = grouped.apply(lambda x: x - x.mean(axis=0))
tm.assert_frame_equal(result, expected)
- ts = ts.T
- msg = "DataFrame.groupby with axis=1 is deprecated"
- with tm.assert_produces_warning(FutureWarning, match=msg):
- grouped = ts.groupby(lambda x: x.weekday(), axis=1, group_keys=False)
- result = ts - grouped.transform("mean")
- expected = grouped.apply(lambda x: (x.T - x.mean(1)).T)
- tm.assert_frame_equal(result, expected)
-
def test_transform_dtype():
# GH 9807
@@ -894,38 +818,6 @@ def test_cython_transform_frame_column(
tm.assert_series_equal(expected, res2)
-def test_transform_with_non_scalar_group():
- # GH 10165
- cols = MultiIndex.from_tuples(
- [
- ("syn", "A"),
- ("foo", "A"),
- ("non", "A"),
- ("syn", "C"),
- ("foo", "C"),
- ("non", "C"),
- ("syn", "T"),
- ("foo", "T"),
- ("non", "T"),
- ("syn", "G"),
- ("foo", "G"),
- ("non", "G"),
- ]
- )
- df = DataFrame(
- np.random.default_rng(2).integers(1, 10, (4, 12)),
- columns=cols,
- index=["A", "C", "G", "T"],
- )
-
- msg = "DataFrame.groupby with axis=1 is deprecated"
- with tm.assert_produces_warning(FutureWarning, match=msg):
- gb = df.groupby(axis=1, level=1)
- msg = "transform must return a scalar value for each group.*"
- with pytest.raises(ValueError, match=msg):
- gb.transform(lambda z: z.div(z.sum(axis=1), axis=0))
-
-
@pytest.mark.parametrize(
"cols,expected",
[
@@ -1330,7 +1222,7 @@ def func(grp):
# Check that the fastpath raises, see _transform_general
obj = gb._obj_with_exclusions
- gen = gb._grouper.get_iterator(obj, axis=gb.axis)
+ gen = gb._grouper.get_iterator(obj)
fast_path, slow_path = gb._define_paths(func)
_, group = next(gen)
diff --git a/pandas/tests/plotting/test_boxplot_method.py b/pandas/tests/plotting/test_boxplot_method.py
index 2470aae78d701..abafad5b1d7da 100644
--- a/pandas/tests/plotting/test_boxplot_method.py
+++ b/pandas/tests/plotting/test_boxplot_method.py
@@ -426,25 +426,6 @@ def test_boxplot_legacy2_return_type(self):
axes = _check_plot_works(grouped.boxplot, subplots=False, return_type="axes")
_check_axes_shape(axes, axes_num=1, layout=(1, 1))
- @pytest.mark.parametrize(
- "subplots, warn, axes_num, layout",
- [[True, UserWarning, 3, (2, 2)], [False, None, 1, (1, 1)]],
- )
- def test_boxplot_legacy3(self, subplots, warn, axes_num, layout):
- tuples = zip(string.ascii_letters[:10], range(10))
- df = DataFrame(
- np.random.default_rng(2).random((10, 3)),
- index=MultiIndex.from_tuples(tuples),
- )
- msg = "DataFrame.groupby with axis=1 is deprecated"
- with tm.assert_produces_warning(FutureWarning, match=msg):
- grouped = df.unstack(level=1).groupby(level=0, axis=1)
- with tm.assert_produces_warning(warn, check_stacklevel=False):
- axes = _check_plot_works(
- grouped.boxplot, subplots=subplots, return_type="axes"
- )
- _check_axes_shape(axes, axes_num=axes_num, layout=layout)
-
def test_grouped_plot_fignums(self):
n = 10
weight = Series(np.random.default_rng(2).normal(166, 20, size=n))
diff --git a/pandas/tests/resample/test_datetime_index.py b/pandas/tests/resample/test_datetime_index.py
index dfcdc2ce26bcf..c5ef0f39ece19 100644
--- a/pandas/tests/resample/test_datetime_index.py
+++ b/pandas/tests/resample/test_datetime_index.py
@@ -639,26 +639,6 @@ def test_resample_ohlc_dataframe(unit):
# df.columns = ['PRICE', 'PRICE']
-def test_resample_dup_index():
- # GH 4812
- # dup columns with resample raising
- df = DataFrame(
- np.random.default_rng(2).standard_normal((4, 12)),
- index=[2000, 2000, 2000, 2000],
- columns=[Period(year=2000, month=i + 1, freq="M") for i in range(12)],
- )
- df.iloc[3, :] = np.nan
- warning_msg = "DataFrame.resample with axis=1 is deprecated."
- with tm.assert_produces_warning(FutureWarning, match=warning_msg):
- result = df.resample("QE", axis=1).mean()
-
- msg = "DataFrame.groupby with axis=1 is deprecated"
- with tm.assert_produces_warning(FutureWarning, match=msg):
- expected = df.groupby(lambda x: int((x.month - 1) / 3), axis=1).mean()
- expected.columns = [Period(year=2000, quarter=i + 1, freq="Q") for i in range(4)]
- tm.assert_frame_equal(result, expected)
-
-
def test_resample_reresample(unit):
dti = date_range(
start=datetime(2005, 1, 1), end=datetime(2005, 1, 10), freq="D"
@@ -737,21 +717,6 @@ def test_asfreq_non_unique(unit):
ts.asfreq("B")
-def test_resample_axis1(unit):
- rng = date_range("1/1/2000", "2/29/2000").as_unit(unit)
- df = DataFrame(
- np.random.default_rng(2).standard_normal((3, len(rng))),
- columns=rng,
- index=["a", "b", "c"],
- )
-
- warning_msg = "DataFrame.resample with axis=1 is deprecated."
- with tm.assert_produces_warning(FutureWarning, match=warning_msg):
- result = df.resample("ME", axis=1).mean()
- expected = df.T.resample("ME").mean().T
- tm.assert_frame_equal(result, expected)
-
-
@pytest.mark.parametrize("freq", ["min", "5min", "15min", "30min", "4h", "12h"])
def test_resample_anchored_ticks(freq, unit):
# If a fixed delta (5 minute, 4 hour) evenly divides a day, we should
diff --git a/pandas/tests/resample/test_resample_api.py b/pandas/tests/resample/test_resample_api.py
index 12abd1c98784b..17c286c4651e6 100644
--- a/pandas/tests/resample/test_resample_api.py
+++ b/pandas/tests/resample/test_resample_api.py
@@ -35,13 +35,13 @@ def test_frame(dti, _test_series):
def test_str(_test_series):
r = _test_series.resample("h")
assert (
- "DatetimeIndexResampler [freq=<Hour>, axis=0, closed=left, "
+ "DatetimeIndexResampler [freq=<Hour>, closed=left, "
"label=left, convention=start, origin=start_day]" in str(r)
)
r = _test_series.resample("h", origin="2000-01-01")
assert (
- "DatetimeIndexResampler [freq=<Hour>, axis=0, closed=left, "
+ "DatetimeIndexResampler [freq=<Hour>, closed=left, "
"label=left, convention=start, origin=2000-01-01 00:00:00]" in str(r)
)
@@ -620,26 +620,6 @@ def test_agg_specificationerror_invalid_names(cases):
cases[["A"]].agg({"A": ["sum", "std"], "B": ["mean", "std"]})
-@pytest.mark.parametrize(
- "func", [["min"], ["mean", "max"], {"A": "sum"}, {"A": "prod", "B": "median"}]
-)
-def test_multi_agg_axis_1_raises(func):
- # GH#46904
-
- index = date_range(datetime(2005, 1, 1), datetime(2005, 1, 10), freq="D")
- index.name = "date"
- df = DataFrame(
- np.random.default_rng(2).random((10, 2)), columns=list("AB"), index=index
- ).T
- warning_msg = "DataFrame.resample with axis=1 is deprecated."
- with tm.assert_produces_warning(FutureWarning, match=warning_msg):
- res = df.resample("ME", axis=1)
- with pytest.raises(
- NotImplementedError, match="axis other than 0 is not supported"
- ):
- res.agg(func)
-
-
def test_agg_nested_dicts():
index = date_range(datetime(2005, 1, 1), datetime(2005, 1, 10), freq="D")
index.name = "date"
@@ -1047,37 +1027,6 @@ def test_args_kwargs_depr(method, raises):
func(*args, 1, 2, 3, 4)
-def test_df_axis_param_depr():
- index = date_range(datetime(2005, 1, 1), datetime(2005, 1, 10), freq="D")
- index.name = "date"
- df = DataFrame(
- np.random.default_rng(2).random((10, 2)), columns=list("AB"), index=index
- ).T
-
- # Deprecation error when axis=1 is explicitly passed
- warning_msg = "DataFrame.resample with axis=1 is deprecated."
- with tm.assert_produces_warning(FutureWarning, match=warning_msg):
- df.resample("ME", axis=1)
-
- # Deprecation error when axis=0 is explicitly passed
- df = df.T
- warning_msg = (
- "The 'axis' keyword in DataFrame.resample is deprecated and "
- "will be removed in a future version."
- )
- with tm.assert_produces_warning(FutureWarning, match=warning_msg):
- df.resample("ME", axis=0)
-
-
-def test_series_axis_param_depr(_test_series):
- warning_msg = (
- "The 'axis' keyword in Series.resample is "
- "deprecated and will be removed in a future version."
- )
- with tm.assert_produces_warning(FutureWarning, match=warning_msg):
- _test_series.resample("h", axis=0)
-
-
def test_resample_empty():
# GH#52484
df = DataFrame(
diff --git a/pandas/tests/resample/test_time_grouper.py b/pandas/tests/resample/test_time_grouper.py
index 3f9340b800eae..c5e202f36659b 100644
--- a/pandas/tests/resample/test_time_grouper.py
+++ b/pandas/tests/resample/test_time_grouper.py
@@ -296,7 +296,7 @@ def test_repr():
# GH18203
result = repr(Grouper(key="A", freq="h"))
expected = (
- "TimeGrouper(key='A', freq=<Hour>, axis=0, sort=True, dropna=True, "
+ "TimeGrouper(key='A', freq=<Hour>, sort=True, dropna=True, "
"closed='left', label='left', how='mean', "
"convention='e', origin='start_day')"
)
@@ -304,7 +304,7 @@ def test_repr():
result = repr(Grouper(key="A", freq="h", origin="2000-01-01"))
expected = (
- "TimeGrouper(key='A', freq=<Hour>, axis=0, sort=True, dropna=True, "
+ "TimeGrouper(key='A', freq=<Hour>, sort=True, dropna=True, "
"closed='left', label='left', how='mean', "
"convention='e', origin=Timestamp('2000-01-01 00:00:00'))"
)
diff --git a/pandas/tests/test_multilevel.py b/pandas/tests/test_multilevel.py
index 6644ec82fab17..fda51b157cd75 100644
--- a/pandas/tests/test_multilevel.py
+++ b/pandas/tests/test_multilevel.py
@@ -28,16 +28,6 @@ def test_reindex_level(self, multiindex_year_month_day_dataframe_random_data):
expected = ymd["A"].groupby(level="month").transform("sum")
tm.assert_series_equal(result, expected, check_names=False)
- # axis=1
- msg = "DataFrame.groupby with axis=1 is deprecated"
- with tm.assert_produces_warning(FutureWarning, match=msg):
- gb = ymd.T.groupby("month", axis=1)
-
- month_sums = gb.sum()
- result = month_sums.reindex(columns=ymd.index, level=1)
- expected = ymd.groupby(level="month").transform("sum").T
- tm.assert_frame_equal(result, expected)
-
def test_reindex(self, multiindex_dataframe_random_data):
frame = multiindex_dataframe_random_data
@@ -97,27 +87,6 @@ def test_groupby_corner(self):
# should work
df.groupby(level="three")
- def test_groupby_level_no_obs(self):
- # #1697
- midx = MultiIndex.from_tuples(
- [
- ("f1", "s1"),
- ("f1", "s2"),
- ("f2", "s1"),
- ("f2", "s2"),
- ("f3", "s1"),
- ("f3", "s2"),
- ]
- )
- df = DataFrame([[1, 2, 3, 4, 5, 6], [7, 8, 9, 10, 11, 12]], columns=midx)
- df1 = df.loc(axis=1)[df.columns.map(lambda u: u[0] in ["f2", "f3"])]
-
- msg = "DataFrame.groupby with axis=1 is deprecated"
- with tm.assert_produces_warning(FutureWarning, match=msg):
- grouped = df1.groupby(axis=1, level=0)
- result = grouped.sum()
- assert (result.columns == ["f2", "f3"]).all()
-
def test_setitem_with_expansion_multiindex_columns(
self, multiindex_year_month_day_dataframe_random_data
):
diff --git a/pandas/tests/window/test_api.py b/pandas/tests/window/test_api.py
index fe2da210c6fe9..b4d555203212e 100644
--- a/pandas/tests/window/test_api.py
+++ b/pandas/tests/window/test_api.py
@@ -127,19 +127,6 @@ def test_agg(step):
tm.assert_frame_equal(result, expected, check_like=True)
-@pytest.mark.parametrize(
- "func", [["min"], ["mean", "max"], {"b": "sum"}, {"b": "prod", "c": "median"}]
-)
-def test_multi_axis_1_raises(func):
- # GH#46904
- df = DataFrame({"a": [1, 1, 2], "b": [3, 4, 5], "c": [6, 7, 8]})
- msg = "Support for axis=1 in DataFrame.rolling is deprecated"
- with tm.assert_produces_warning(FutureWarning, match=msg):
- r = df.rolling(window=3, axis=1)
- with pytest.raises(NotImplementedError, match="axis other than 0 is not supported"):
- r.agg(func)
-
-
def test_agg_apply(raw):
# passed lambda
df = DataFrame({"A": range(5), "B": range(0, 10, 2)})
@@ -352,32 +339,6 @@ def test_dont_modify_attributes_after_methods(
assert result == expected
-def test_centered_axis_validation(step):
- # ok
- msg = "The 'axis' keyword in Series.rolling is deprecated"
- with tm.assert_produces_warning(FutureWarning, match=msg):
- Series(np.ones(10)).rolling(window=3, center=True, axis=0, step=step).mean()
-
- # bad axis
- msg = "No axis named 1 for object type Series"
- with pytest.raises(ValueError, match=msg):
- Series(np.ones(10)).rolling(window=3, center=True, axis=1, step=step).mean()
-
- # ok ok
- df = DataFrame(np.ones((10, 10)))
- msg = "The 'axis' keyword in DataFrame.rolling is deprecated"
- with tm.assert_produces_warning(FutureWarning, match=msg):
- df.rolling(window=3, center=True, axis=0, step=step).mean()
- msg = "Support for axis=1 in DataFrame.rolling is deprecated"
- with tm.assert_produces_warning(FutureWarning, match=msg):
- df.rolling(window=3, center=True, axis=1, step=step).mean()
-
- # bad axis
- msg = "No axis named 2 for object type DataFrame"
- with pytest.raises(ValueError, match=msg):
- (df.rolling(window=3, center=True, axis=2, step=step).mean())
-
-
def test_rolling_min_min_periods(step):
a = Series([1, 2, 3, 4, 5])
result = a.rolling(window=100, min_periods=1, step=step).min()
diff --git a/pandas/tests/window/test_apply.py b/pandas/tests/window/test_apply.py
index 136f81632cb0a..2398713585cfb 100644
--- a/pandas/tests/window/test_apply.py
+++ b/pandas/tests/window/test_apply.py
@@ -316,13 +316,3 @@ def test_center_reindex_frame(raw):
)
frame_rs = frame.rolling(window=25, min_periods=minp, center=True).apply(f, raw=raw)
tm.assert_frame_equal(frame_xp, frame_rs)
-
-
-def test_axis1(raw):
- # GH 45912
- df = DataFrame([1, 2])
- msg = "Support for axis=1 in DataFrame.rolling is deprecated"
- with tm.assert_produces_warning(FutureWarning, match=msg):
- result = df.rolling(window=1, axis=1).apply(np.sum, raw=raw)
- expected = DataFrame([1.0, 2.0])
- tm.assert_frame_equal(result, expected)
diff --git a/pandas/tests/window/test_ewm.py b/pandas/tests/window/test_ewm.py
index d7c72105a673b..2e2cfa156019f 100644
--- a/pandas/tests/window/test_ewm.py
+++ b/pandas/tests/window/test_ewm.py
@@ -158,56 +158,6 @@ def test_ewma_times_adjust_false_raises():
)
-@pytest.mark.parametrize(
- "func, expected",
- [
- [
- "mean",
- DataFrame(
- {
- 0: range(5),
- 1: range(4, 9),
- 2: [7.428571, 9, 10.571429, 12.142857, 13.714286],
- },
- dtype=float,
- ),
- ],
- [
- "std",
- DataFrame(
- {
- 0: [np.nan] * 5,
- 1: [4.242641] * 5,
- 2: [4.6291, 5.196152, 5.781745, 6.380775, 6.989788],
- }
- ),
- ],
- [
- "var",
- DataFrame(
- {
- 0: [np.nan] * 5,
- 1: [18.0] * 5,
- 2: [21.428571, 27, 33.428571, 40.714286, 48.857143],
- }
- ),
- ],
- ],
-)
-def test_float_dtype_ewma(func, expected, float_numpy_dtype):
- # GH#42452
-
- df = DataFrame(
- {0: range(5), 1: range(6, 11), 2: range(10, 20, 2)}, dtype=float_numpy_dtype
- )
- msg = "Support for axis=1 in DataFrame.ewm is deprecated"
- with tm.assert_produces_warning(FutureWarning, match=msg):
- e = df.ewm(alpha=0.5, axis=1)
- result = getattr(e, func)()
-
- tm.assert_frame_equal(result, expected)
-
-
def test_times_string_col_raises():
# GH 43265
df = DataFrame(
diff --git a/pandas/tests/window/test_expanding.py b/pandas/tests/window/test_expanding.py
index 9174307cec5d1..6d452b27f3654 100644
--- a/pandas/tests/window/test_expanding.py
+++ b/pandas/tests/window/test_expanding.py
@@ -79,23 +79,14 @@ def test_missing_minp_zero():
tm.assert_series_equal(result, expected)
-def test_expanding_axis(axis):
+def test_expanding():
# see gh-23372.
df = DataFrame(np.ones((10, 20)))
- axis = df._get_axis_number(axis)
- if axis == 0:
- msg = "The 'axis' keyword in DataFrame.expanding is deprecated"
- expected = DataFrame(
- {i: [np.nan] * 2 + [float(j) for j in range(3, 11)] for i in range(20)}
- )
- else:
- # axis == 1
- msg = "Support for axis=1 in DataFrame.expanding is deprecated"
- expected = DataFrame([[np.nan] * 2 + [float(i) for i in range(3, 21)]] * 10)
-
- with tm.assert_produces_warning(FutureWarning, match=msg):
- result = df.expanding(3, axis=axis).sum()
+ expected = DataFrame(
+ {i: [np.nan] * 2 + [float(j) for j in range(3, 11)] for i in range(20)}
+ )
+ result = df.expanding(3).sum()
tm.assert_frame_equal(result, expected)
@@ -329,9 +320,7 @@ def test_expanding_corr_pairwise(frame):
def test_expanding_func(func, static_comp, frame_or_series):
data = frame_or_series(np.array(list(range(10)) + [np.nan] * 10))
- msg = "The 'axis' keyword in (Series|DataFrame).expanding is deprecated"
- with tm.assert_produces_warning(FutureWarning, match=msg):
- obj = data.expanding(min_periods=1, axis=0)
+ obj = data.expanding(min_periods=1)
result = getattr(obj, func)()
assert isinstance(result, frame_or_series)
@@ -355,33 +344,26 @@ def test_expanding_func(func, static_comp, frame_or_series):
def test_expanding_min_periods(func, static_comp):
ser = Series(np.random.default_rng(2).standard_normal(50))
- msg = "The 'axis' keyword in Series.expanding is deprecated"
- with tm.assert_produces_warning(FutureWarning, match=msg):
- result = getattr(ser.expanding(min_periods=30, axis=0), func)()
+ result = getattr(ser.expanding(min_periods=30), func)()
assert result[:29].isna().all()
tm.assert_almost_equal(result.iloc[-1], static_comp(ser[:50]))
# min_periods is working correctly
- with tm.assert_produces_warning(FutureWarning, match=msg):
- result = getattr(ser.expanding(min_periods=15, axis=0), func)()
+ result = getattr(ser.expanding(min_periods=15), func)()
assert isna(result.iloc[13])
assert notna(result.iloc[14])
ser2 = Series(np.random.default_rng(2).standard_normal(20))
- with tm.assert_produces_warning(FutureWarning, match=msg):
- result = getattr(ser2.expanding(min_periods=5, axis=0), func)()
+ result = getattr(ser2.expanding(min_periods=5), func)()
assert isna(result[3])
assert notna(result[4])
# min_periods=0
- with tm.assert_produces_warning(FutureWarning, match=msg):
- result0 = getattr(ser.expanding(min_periods=0, axis=0), func)()
- with tm.assert_produces_warning(FutureWarning, match=msg):
- result1 = getattr(ser.expanding(min_periods=1, axis=0), func)()
+ result0 = getattr(ser.expanding(min_periods=0), func)()
+ result1 = getattr(ser.expanding(min_periods=1), func)()
tm.assert_almost_equal(result0, result1)
- with tm.assert_produces_warning(FutureWarning, match=msg):
- result = getattr(ser.expanding(min_periods=1, axis=0), func)()
+ result = getattr(ser.expanding(min_periods=1), func)()
tm.assert_almost_equal(result.iloc[-1], static_comp(ser[:50]))
diff --git a/pandas/tests/window/test_numba.py b/pandas/tests/window/test_numba.py
index 139e1ff7f65fd..650eb911e410b 100644
--- a/pandas/tests/window/test_numba.py
+++ b/pandas/tests/window/test_numba.py
@@ -328,7 +328,6 @@ def f(x):
def test_table_method_rolling_methods(
self,
- axis,
nogil,
parallel,
nopython,
@@ -340,16 +339,14 @@ def test_table_method_rolling_methods(
engine_kwargs = {"nogil": nogil, "parallel": parallel, "nopython": nopython}
df = DataFrame(np.eye(3))
- roll_table = df.rolling(2, method="table", axis=axis, min_periods=0, step=step)
+ roll_table = df.rolling(2, method="table", min_periods=0, step=step)
if method in ("var", "std"):
with pytest.raises(NotImplementedError, match=f"{method} not supported"):
getattr(roll_table, method)(
engine_kwargs=engine_kwargs, engine="numba", **kwargs
)
else:
- roll_single = df.rolling(
- 2, method="single", axis=axis, min_periods=0, step=step
- )
+ roll_single = df.rolling(2, method="single", min_periods=0, step=step)
result = getattr(roll_table, method)(
engine_kwargs=engine_kwargs, engine="numba", **kwargs
)
@@ -358,19 +355,19 @@ def test_table_method_rolling_methods(
)
tm.assert_frame_equal(result, expected)
- def test_table_method_rolling_apply(self, axis, nogil, parallel, nopython, step):
+ def test_table_method_rolling_apply(self, nogil, parallel, nopython, step):
engine_kwargs = {"nogil": nogil, "parallel": parallel, "nopython": nopython}
def f(x):
return np.sum(x, axis=0) + 1
df = DataFrame(np.eye(3))
- result = df.rolling(
- 2, method="table", axis=axis, min_periods=0, step=step
- ).apply(f, raw=True, engine_kwargs=engine_kwargs, engine="numba")
- expected = df.rolling(
- 2, method="single", axis=axis, min_periods=0, step=step
- ).apply(f, raw=True, engine_kwargs=engine_kwargs, engine="numba")
+ result = df.rolling(2, method="table", min_periods=0, step=step).apply(
+ f, raw=True, engine_kwargs=engine_kwargs, engine="numba"
+ )
+ expected = df.rolling(2, method="single", min_periods=0, step=step).apply(
+ f, raw=True, engine_kwargs=engine_kwargs, engine="numba"
+ )
tm.assert_frame_equal(result, expected)
def test_table_method_rolling_weighted_mean(self, step):
@@ -393,37 +390,37 @@ def weighted_mean(x):
)[::step]
tm.assert_frame_equal(result, expected)
- def test_table_method_expanding_apply(self, axis, nogil, parallel, nopython):
+ def test_table_method_expanding_apply(self, nogil, parallel, nopython):
engine_kwargs = {"nogil": nogil, "parallel": parallel, "nopython": nopython}
def f(x):
return np.sum(x, axis=0) + 1
df = DataFrame(np.eye(3))
- result = df.expanding(method="table", axis=axis).apply(
+ result = df.expanding(method="table").apply(
f, raw=True, engine_kwargs=engine_kwargs, engine="numba"
)
- expected = df.expanding(method="single", axis=axis).apply(
+ expected = df.expanding(method="single").apply(
f, raw=True, engine_kwargs=engine_kwargs, engine="numba"
)
tm.assert_frame_equal(result, expected)
def test_table_method_expanding_methods(
- self, axis, nogil, parallel, nopython, arithmetic_numba_supported_operators
+ self, nogil, parallel, nopython, arithmetic_numba_supported_operators
):
method, kwargs = arithmetic_numba_supported_operators
engine_kwargs = {"nogil": nogil, "parallel": parallel, "nopython": nopython}
df = DataFrame(np.eye(3))
- expand_table = df.expanding(method="table", axis=axis)
+ expand_table = df.expanding(method="table")
if method in ("var", "std"):
with pytest.raises(NotImplementedError, match=f"{method} not supported"):
getattr(expand_table, method)(
engine_kwargs=engine_kwargs, engine="numba", **kwargs
)
else:
- expand_single = df.expanding(method="single", axis=axis)
+ expand_single = df.expanding(method="single")
result = getattr(expand_table, method)(
engine_kwargs=engine_kwargs, engine="numba", **kwargs
)
@@ -434,15 +431,15 @@ def test_table_method_expanding_methods(
@pytest.mark.parametrize("data", [np.eye(3), np.ones((2, 3)), np.ones((3, 2))])
@pytest.mark.parametrize("method", ["mean", "sum"])
- def test_table_method_ewm(self, data, method, axis, nogil, parallel, nopython):
+ def test_table_method_ewm(self, data, method, nogil, parallel, nopython):
engine_kwargs = {"nogil": nogil, "parallel": parallel, "nopython": nopython}
df = DataFrame(data)
- result = getattr(df.ewm(com=1, method="table", axis=axis), method)(
+ result = getattr(df.ewm(com=1, method="table"), method)(
engine_kwargs=engine_kwargs, engine="numba"
)
- expected = getattr(df.ewm(com=1, method="single", axis=axis), method)(
+ expected = getattr(df.ewm(com=1, method="single"), method)(
engine_kwargs=engine_kwargs, engine="numba"
)
tm.assert_frame_equal(result, expected)
diff --git a/pandas/tests/window/test_rolling.py b/pandas/tests/window/test_rolling.py
index 0ca6bf0de94dd..fda631987255a 100644
--- a/pandas/tests/window/test_rolling.py
+++ b/pandas/tests/window/test_rolling.py
@@ -21,8 +21,6 @@
Timestamp,
date_range,
period_range,
- to_datetime,
- to_timedelta,
)
import pandas._testing as tm
from pandas.api.indexers import BaseIndexer
@@ -594,39 +592,20 @@ def test_multi_index_names():
assert result.index.names == [None, "1", "2"]
-def test_rolling_axis_sum(axis):
+def test_rolling_axis_sum():
# see gh-23372.
df = DataFrame(np.ones((10, 20)))
- axis = df._get_axis_number(axis)
-
- if axis == 0:
- msg = "The 'axis' keyword in DataFrame.rolling"
- expected = DataFrame({i: [np.nan] * 2 + [3.0] * 8 for i in range(20)})
- else:
- # axis == 1
- msg = "Support for axis=1 in DataFrame.rolling is deprecated"
- expected = DataFrame([[np.nan] * 2 + [3.0] * 18] * 10)
-
- with tm.assert_produces_warning(FutureWarning, match=msg):
- result = df.rolling(3, axis=axis).sum()
+ expected = DataFrame({i: [np.nan] * 2 + [3.0] * 8 for i in range(20)})
+ result = df.rolling(3).sum()
tm.assert_frame_equal(result, expected)
-def test_rolling_axis_count(axis):
+def test_rolling_axis_count():
# see gh-26055
df = DataFrame({"x": range(3), "y": range(3)})
- axis = df._get_axis_number(axis)
-
- if axis in [0, "index"]:
- msg = "The 'axis' keyword in DataFrame.rolling"
- expected = DataFrame({"x": [1.0, 2.0, 2.0], "y": [1.0, 2.0, 2.0]})
- else:
- msg = "Support for axis=1 in DataFrame.rolling is deprecated"
- expected = DataFrame({"x": [1.0, 1.0, 1.0], "y": [2.0, 2.0, 2.0]})
-
- with tm.assert_produces_warning(FutureWarning, match=msg):
- result = df.rolling(2, axis=axis, min_periods=0).count()
+ expected = DataFrame({"x": [1.0, 2.0, 2.0], "y": [1.0, 2.0, 2.0]})
+ result = df.rolling(2, min_periods=0).count()
tm.assert_frame_equal(result, expected)
@@ -639,21 +618,14 @@ def test_readonly_array():
tm.assert_series_equal(result, expected)
-def test_rolling_datetime(axis, tz_naive_fixture):
+def test_rolling_datetime(tz_naive_fixture):
# GH-28192
tz = tz_naive_fixture
df = DataFrame(
{i: [1] * 2 for i in date_range("2019-8-01", "2019-08-03", freq="D", tz=tz)}
)
- if axis in [0, "index"]:
- msg = "The 'axis' keyword in DataFrame.rolling"
- with tm.assert_produces_warning(FutureWarning, match=msg):
- result = df.T.rolling("2D", axis=axis).sum().T
- else:
- msg = "Support for axis=1 in DataFrame.rolling"
- with tm.assert_produces_warning(FutureWarning, match=msg):
- result = df.rolling("2D", axis=axis).sum()
+ result = df.T.rolling("2D").sum().T
expected = DataFrame(
{
**{
@@ -1065,75 +1037,6 @@ def test_rolling_numerical_too_large_numbers():
tm.assert_series_equal(result, expected)
-@pytest.mark.parametrize(
- ("func", "value"),
- [("sum", 2.0), ("max", 1.0), ("min", 1.0), ("mean", 1.0), ("median", 1.0)],
-)
-def test_rolling_mixed_dtypes_axis_1(func, value):
- # GH: 20649
- df = DataFrame(1, index=[1, 2], columns=["a", "b", "c"])
- df["c"] = 1.0
- msg = "Support for axis=1 in DataFrame.rolling is deprecated"
- with tm.assert_produces_warning(FutureWarning, match=msg):
- roll = df.rolling(window=2, min_periods=1, axis=1)
- result = getattr(roll, func)()
- expected = DataFrame(
- {"a": [1.0, 1.0], "b": [value, value], "c": [value, value]},
- index=[1, 2],
- )
- tm.assert_frame_equal(result, expected)
-
-
-def test_rolling_axis_one_with_nan():
- # GH: 35596
- df = DataFrame(
- [
- [0, 1, 2, 4, np.nan, np.nan, np.nan],
- [0, 1, 2, np.nan, np.nan, np.nan, np.nan],
- [0, 2, 2, np.nan, 2, np.nan, 1],
- ]
- )
- msg = "Support for axis=1 in DataFrame.rolling is deprecated"
- with tm.assert_produces_warning(FutureWarning, match=msg):
- result = df.rolling(window=7, min_periods=1, axis="columns").sum()
- expected = DataFrame(
- [
- [0.0, 1.0, 3.0, 7.0, 7.0, 7.0, 7.0],
- [0.0, 1.0, 3.0, 3.0, 3.0, 3.0, 3.0],
- [0.0, 2.0, 4.0, 4.0, 6.0, 6.0, 7.0],
- ]
- )
- tm.assert_frame_equal(result, expected)
-
-
-@pytest.mark.parametrize(
- "value",
- ["test", to_datetime("2019-12-31"), to_timedelta("1 days 06:05:01.00003")],
-)
-def test_rolling_axis_1_non_numeric_dtypes(value):
- # GH: 20649
- df = DataFrame({"a": [1, 2]})
- df["b"] = value
- msg = "Support for axis=1 in DataFrame.rolling is deprecated"
- with tm.assert_produces_warning(FutureWarning, match=msg):
- result = df.rolling(window=2, min_periods=1, axis=1).sum()
- expected = DataFrame({"a": [1.0, 2.0]})
- tm.assert_frame_equal(result, expected)
-
-
-def test_rolling_on_df_transposed():
- # GH: 32724
- df = DataFrame({"A": [1, None], "B": [4, 5], "C": [7, 8]})
- expected = DataFrame({"A": [1.0, np.nan], "B": [5.0, 5.0], "C": [11.0, 13.0]})
- msg = "Support for axis=1 in DataFrame.rolling is deprecated"
- with tm.assert_produces_warning(FutureWarning, match=msg):
- result = df.rolling(min_periods=1, window=2, axis=1).sum()
- tm.assert_frame_equal(result, expected)
-
- result = df.T.rolling(min_periods=1, window=2).sum().T
- tm.assert_frame_equal(result, expected)
-
-
@pytest.mark.parametrize(
("index", "window"),
[
@@ -1576,56 +1479,6 @@ def test_rolling_zero_window():
tm.assert_series_equal(result, expected)
-def test_rolling_float_dtype(float_numpy_dtype):
- # GH#42452
- df = DataFrame({"A": range(5), "B": range(10, 15)}, dtype=float_numpy_dtype)
- expected = DataFrame(
- {"A": [np.nan] * 5, "B": range(10, 20, 2)},
- dtype=float_numpy_dtype,
- )
- msg = "Support for axis=1 in DataFrame.rolling is deprecated"
- with tm.assert_produces_warning(FutureWarning, match=msg):
- result = df.rolling(2, axis=1).sum()
- tm.assert_frame_equal(result, expected, check_dtype=False)
-
-
-def test_rolling_numeric_dtypes():
- # GH#41779
- df = DataFrame(np.arange(40).reshape(4, 10), columns=list("abcdefghij")).astype(
- {
- "a": "float16",
- "b": "float32",
- "c": "float64",
- "d": "int8",
- "e": "int16",
- "f": "int32",
- "g": "uint8",
- "h": "uint16",
- "i": "uint32",
- "j": "uint64",
- }
- )
- msg = "Support for axis=1 in DataFrame.rolling is deprecated"
- with tm.assert_produces_warning(FutureWarning, match=msg):
- result = df.rolling(window=2, min_periods=1, axis=1).min()
- expected = DataFrame(
- {
- "a": range(0, 40, 10),
- "b": range(0, 40, 10),
- "c": range(1, 40, 10),
- "d": range(2, 40, 10),
- "e": range(3, 40, 10),
- "f": range(4, 40, 10),
- "g": range(5, 40, 10),
- "h": range(6, 40, 10),
- "i": range(7, 40, 10),
- "j": range(8, 40, 10),
- },
- dtype="float64",
- )
- tm.assert_frame_equal(result, expected)
-
-
@pytest.mark.parametrize("window", [1, 3, 10, 20])
@pytest.mark.parametrize("method", ["min", "max", "average"])
@pytest.mark.parametrize("pct", [True, False])
diff --git a/pandas/tests/window/test_timeseries_window.py b/pandas/tests/window/test_timeseries_window.py
index bd0fadeb3e475..820b0134cc577 100644
--- a/pandas/tests/window/test_timeseries_window.py
+++ b/pandas/tests/window/test_timeseries_window.py
@@ -689,17 +689,11 @@ def test_rolling_on_multi_index_level(self):
tm.assert_frame_equal(result, expected)
-@pytest.mark.parametrize("msg, axis", [["column", 1], ["index", 0]])
-def test_nat_axis_error(msg, axis):
+def test_nat_axis_error():
idx = [Timestamp("2020"), NaT]
- kwargs = {"columns" if axis == 1 else "index": idx}
- df = DataFrame(np.eye(2), **kwargs)
- warn_msg = "The 'axis' keyword in DataFrame.rolling is deprecated"
- if axis == 1:
- warn_msg = "Support for axis=1 in DataFrame.rolling is deprecated"
- with pytest.raises(ValueError, match=f"{msg} values must not have NaT"):
- with tm.assert_produces_warning(FutureWarning, match=warn_msg):
- df.rolling("D", axis=axis).mean()
+ df = DataFrame(np.eye(2), index=idx)
+ with pytest.raises(ValueError, match="index values must not have NaT"):
+ df.rolling("D").mean()
@td.skip_if_no("pyarrow")
diff --git a/pandas/tests/window/test_win_type.py b/pandas/tests/window/test_win_type.py
index 5c785ed3fccb2..574dfc34b6d26 100644
--- a/pandas/tests/window/test_win_type.py
+++ b/pandas/tests/window/test_win_type.py
@@ -668,20 +668,3 @@ def test_weighted_var_big_window_no_segfault(win_types, center):
expected = Series(np.nan)
tm.assert_series_equal(result, expected)
-
-
-def test_rolling_center_axis_1():
- pytest.importorskip("scipy")
- df = DataFrame(
- {"a": [1, 1, 0, 0, 0, 1], "b": [1, 0, 0, 1, 0, 0], "c": [1, 0, 0, 1, 0, 1]}
- )
-
- msg = "Support for axis=1 in DataFrame.rolling is deprecated"
- with tm.assert_produces_warning(FutureWarning, match=msg):
- result = df.rolling(window=3, axis=1, win_type="boxcar", center=True).sum()
-
- expected = DataFrame(
- {"a": [np.nan] * 6, "b": [3.0, 1.0, 0.0, 2.0, 0.0, 2.0], "c": [np.nan] * 6}
- )
-
- tm.assert_frame_equal(result, expected, check_dtype=True)
| - [ ] closes #xxxx (Replace xxxx with the GitHub issue number)
- [ ] [Tests added and passed](https://pandas.pydata.org/pandas-docs/dev/development/contributing_codebase.html#writing-tests) if fixing a bug or adding a new feature
- [ ] All [code checks passed](https://pandas.pydata.org/pandas-docs/dev/development/contributing_codebase.html#pre-commit).
- [ ] Added [type annotations](https://pandas.pydata.org/pandas-docs/dev/development/contributing_codebase.html#type-hints) to new arguments/methods/functions.
- [ ] Added an entry in the latest `doc/source/whatsnew/vX.X.X.rst` file if fixing a bug or adding a new feature.
Enforcing #51203 | https://api.github.com/repos/pandas-dev/pandas/pulls/57186 | 2024-01-31T21:55:30Z | 2024-02-01T17:39:51Z | 2024-02-01T17:39:51Z | 2024-02-01T21:37:20Z |
CI: Fix generating requirements-dev.txt | diff --git a/requirements-dev.txt b/requirements-dev.txt
index 13691d114ece4..162e6caebcd8a 100644
--- a/requirements-dev.txt
+++ b/requirements-dev.txt
@@ -3,7 +3,7 @@
pip
versioneer[toml]
-cython~==3.0.5
+cython~=3.0.5
meson[ninja]==1.2.1
meson-python==0.13.1
pytest>=7.3.2
diff --git a/scripts/generate_pip_deps_from_conda.py b/scripts/generate_pip_deps_from_conda.py
index 5fcf09cd073fe..d54d35bc0171f 100755
--- a/scripts/generate_pip_deps_from_conda.py
+++ b/scripts/generate_pip_deps_from_conda.py
@@ -45,7 +45,7 @@ def conda_package_to_pip(package: str):
- A package requiring a specific version, in conda is defined with a single
equal (e.g. ``pandas=1.0``) and in pip with two (e.g. ``pandas==1.0``)
"""
- package = re.sub("(?<=[^<>])=", "==", package).strip()
+ package = re.sub("(?<=[^<>~])=", "==", package).strip()
for compare in ("<=", ">=", "=="):
if compare in package:
| - [ ] closes #xxxx (Replace xxxx with the GitHub issue number)
- [ ] [Tests added and passed](https://pandas.pydata.org/pandas-docs/dev/development/contributing_codebase.html#writing-tests) if fixing a bug or adding a new feature
- [ ] All [code checks passed](https://pandas.pydata.org/pandas-docs/dev/development/contributing_codebase.html#pre-commit).
- [ ] Added [type annotations](https://pandas.pydata.org/pandas-docs/dev/development/contributing_codebase.html#type-hints) to new arguments/methods/functions.
- [ ] Added an entry in the latest `doc/source/whatsnew/vX.X.X.rst` file if fixing a bug or adding a new feature.
Fix for #56993 | https://api.github.com/repos/pandas-dev/pandas/pulls/57185 | 2024-01-31T21:47:11Z | 2024-01-31T22:10:02Z | 2024-01-31T22:10:02Z | 2024-01-31T22:17:58Z |
Use ruff to detect banned import | diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml
index ccd01abc4affe..b7e43404b86bd 100644
--- a/.pre-commit-config.yaml
+++ b/.pre-commit-config.yaml
@@ -299,13 +299,6 @@ repos:
files: ^pandas/core/
exclude: ^pandas/core/api\.py$
types: [python]
- - id: use-io-common-urlopen
- name: Use pandas.io.common.urlopen instead of urllib.request.urlopen
- language: python
- entry: python scripts/use_io_common_urlopen.py
- files: ^pandas/
- exclude: ^pandas/tests/
- types: [python]
- id: no-bool-in-core-generic
name: Use bool_t instead of bool in pandas/core/generic.py
entry: python scripts/no_bool_in_generic.py
diff --git a/pandas/io/common.py b/pandas/io/common.py
index 16d7cb76f9ce9..682780a409a8b 100644
--- a/pandas/io/common.py
+++ b/pandas/io/common.py
@@ -286,7 +286,7 @@ def urlopen(*args, **kwargs):
"""
import urllib.request
- return urllib.request.urlopen(*args, **kwargs)
+ return urllib.request.urlopen(*args, **kwargs) # noqa: TID251
def is_fsspec_url(url: FilePath | BaseBuffer) -> bool:
diff --git a/pyproject.toml b/pyproject.toml
index 934f66136f601..7614ceecbd8ca 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -330,6 +330,9 @@ exclude = [
"env",
]
+[tool.ruff.lint.flake8-tidy-imports.banned-api]
+"urllib.request.urlopen".msg = "Use pandas.io.common.urlopen instead of urllib.request.urlopen"
+
[tool.ruff.per-file-ignores]
# relative imports allowed for asv_bench
"asv_bench/*" = ["TID", "NPY002"]
diff --git a/scripts/tests/test_use_io_common_urlopen.py b/scripts/tests/test_use_io_common_urlopen.py
deleted file mode 100644
index c2c4a7fe9cb58..0000000000000
--- a/scripts/tests/test_use_io_common_urlopen.py
+++ /dev/null
@@ -1,23 +0,0 @@
-import pytest
-
-from scripts.use_io_common_urlopen import use_io_common_urlopen
-
-PATH = "t.py"
-
-
-def test_inconsistent_usage(capsys) -> None:
- content = "from urllib.request import urlopen"
- result_msg = (
- "t.py:1:0: Don't use urllib.request.urlopen, "
- "use pandas.io.common.urlopen instead\n"
- )
- with pytest.raises(SystemExit, match=None):
- use_io_common_urlopen(content, PATH)
- expected_msg, _ = capsys.readouterr()
- assert result_msg == expected_msg
-
-
-def test_consistent_usage() -> None:
- # should not raise
- content = "from pandas.io.common import urlopen"
- use_io_common_urlopen(content, PATH)
diff --git a/scripts/use_io_common_urlopen.py b/scripts/use_io_common_urlopen.py
deleted file mode 100644
index ade97f53cd827..0000000000000
--- a/scripts/use_io_common_urlopen.py
+++ /dev/null
@@ -1,67 +0,0 @@
-"""
-Check that pandas/core imports pandas.array as pd_array.
-
-This makes it easier to grep for usage of pandas array.
-
-This is meant to be run as a pre-commit hook - to run it manually, you can do:
-
- pre-commit run use-io-common-urlopen --all-files
-
-"""
-
-from __future__ import annotations
-
-import argparse
-import ast
-import sys
-from typing import TYPE_CHECKING
-
-if TYPE_CHECKING:
- from collections.abc import Sequence
-
-
-ERROR_MESSAGE = (
- "{path}:{lineno}:{col_offset}: "
- "Don't use urllib.request.urlopen, use pandas.io.common.urlopen instead\n"
-)
-
-
-class Visitor(ast.NodeVisitor):
- def __init__(self, path: str) -> None:
- self.path = path
-
- def visit_ImportFrom(self, node: ast.ImportFrom) -> None:
- # Check that pandas.io.common.urlopen is used instead of
- # urllib.request.urlopen
- if (
- node.module is not None
- and node.module.startswith("urllib.request")
- and any(i.name == "urlopen" for i in node.names)
- ):
- msg = ERROR_MESSAGE.format(
- path=self.path, lineno=node.lineno, col_offset=node.col_offset
- )
- sys.stdout.write(msg)
- sys.exit(1)
- super().generic_visit(node)
-
-
-def use_io_common_urlopen(content: str, path: str) -> None:
- tree = ast.parse(content)
- visitor = Visitor(path)
- visitor.visit(tree)
-
-
-def main(argv: Sequence[str] | None = None) -> None:
- parser = argparse.ArgumentParser()
- parser.add_argument("paths", nargs="*")
- args = parser.parse_args(argv)
-
- for path in args.paths:
- with open(path, encoding="utf-8") as fd:
- content = fd.read()
- use_io_common_urlopen(content, path)
-
-
-if __name__ == "__main__":
- main()
| There are several other potential replacement, I just open this PR first to check if this replacement is good or not. The newly added `noqa` in `pandas/io/common.py` shows that the check works.
- [ ] closes #xxxx (Replace xxxx with the GitHub issue number)
- [ ] [Tests added and passed](https://pandas.pydata.org/pandas-docs/dev/development/contributing_codebase.html#writing-tests) if fixing a bug or adding a new feature
- [ ] All [code checks passed](https://pandas.pydata.org/pandas-docs/dev/development/contributing_codebase.html#pre-commit).
- [ ] Added [type annotations](https://pandas.pydata.org/pandas-docs/dev/development/contributing_codebase.html#type-hints) to new arguments/methods/functions.
- [ ] Added an entry in the latest `doc/source/whatsnew/vX.X.X.rst` file if fixing a bug or adding a new feature.
| https://api.github.com/repos/pandas-dev/pandas/pulls/57184 | 2024-01-31T21:22:40Z | 2024-02-05T17:31:50Z | 2024-02-05T17:31:50Z | 2024-03-17T07:53:18Z |
Migrate pylint to ruff | diff --git a/.github/workflows/code-checks.yml b/.github/workflows/code-checks.yml
index 24b2de251ce8e..937af7e49c6d3 100644
--- a/.github/workflows/code-checks.yml
+++ b/.github/workflows/code-checks.yml
@@ -85,7 +85,7 @@ jobs:
echo "PYTHONPATH=$PYTHONPATH" >> $GITHUB_ENV
if: ${{ steps.build.outcome == 'success' && always() }}
- - name: Typing + pylint
+ - name: Typing
uses: pre-commit/action@v3.0.1
with:
extra_args: --verbose --hook-stage manual --all-files
diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml
index 8eec5d5515239..8c546ccae41e5 100644
--- a/.pre-commit-config.yaml
+++ b/.pre-commit-config.yaml
@@ -16,7 +16,7 @@ ci:
autofix_prs: false
autoupdate_schedule: monthly
# manual stage hooks
- skip: [pylint, pyright, mypy]
+ skip: [pyright, mypy]
repos:
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.3.4
@@ -67,25 +67,6 @@ repos:
- id: fix-encoding-pragma
args: [--remove]
- id: trailing-whitespace
-- repo: https://github.com/pylint-dev/pylint
- rev: v3.0.1
- hooks:
- - id: pylint
- stages: [manual]
- args: [--load-plugins=pylint.extensions.redefined_loop_name, --fail-on=I0021]
- - id: pylint
- alias: redefined-outer-name
- name: Redefining name from outer scope
- files: ^pandas/
- exclude: |
- (?x)
- ^pandas/tests # keep excluded
- |/_testing/ # keep excluded
- |^pandas/util/_test_decorators\.py # keep excluded
- |^pandas/_version\.py # keep excluded
- |^pandas/conftest\.py # keep excluded
- args: [--disable=all, --enable=redefined-outer-name]
- stages: [manual]
- repo: https://github.com/PyCQA/isort
rev: 5.13.2
hooks:
diff --git a/pandas/_libs/tslibs/__init__.py b/pandas/_libs/tslibs/__init__.py
index 88a9a259ac8ec..31979b293a940 100644
--- a/pandas/_libs/tslibs/__init__.py
+++ b/pandas/_libs/tslibs/__init__.py
@@ -36,7 +36,7 @@
"is_supported_dtype",
]
-from pandas._libs.tslibs import dtypes # pylint: disable=import-self
+from pandas._libs.tslibs import dtypes
from pandas._libs.tslibs.conversion import localize_pydatetime
from pandas._libs.tslibs.dtypes import (
Resolution,
diff --git a/pandas/core/arrays/categorical.py b/pandas/core/arrays/categorical.py
index 3af4c528ceae8..8d6880fc2acb3 100644
--- a/pandas/core/arrays/categorical.py
+++ b/pandas/core/arrays/categorical.py
@@ -2482,7 +2482,6 @@ def unique(self) -> Self:
['b', 'a']
Categories (3, object): ['a' < 'b' < 'c']
"""
- # pylint: disable=useless-parent-delegation
return super().unique()
def _cast_quantile_result(self, res_values: np.ndarray) -> np.ndarray:
diff --git a/pandas/core/arrays/datetimelike.py b/pandas/core/arrays/datetimelike.py
index c9aeaa1ce21c3..f4f076103d8c3 100644
--- a/pandas/core/arrays/datetimelike.py
+++ b/pandas/core/arrays/datetimelike.py
@@ -506,7 +506,6 @@ def view(self, dtype: Literal["m8[ns]"]) -> TimedeltaArray: ...
@overload
def view(self, dtype: Dtype | None = ...) -> ArrayLike: ...
- # pylint: disable-next=useless-parent-delegation
def view(self, dtype: Dtype | None = None) -> ArrayLike:
# we need to explicitly call super() method as long as the `@overload`s
# are present in this file.
diff --git a/pandas/core/base.py b/pandas/core/base.py
index f5eefe1b4ab92..95b203590b393 100644
--- a/pandas/core/base.py
+++ b/pandas/core/base.py
@@ -127,7 +127,7 @@ def __sizeof__(self) -> int:
"""
memory_usage = getattr(self, "memory_usage", None)
if memory_usage:
- mem = memory_usage(deep=True) # pylint: disable=not-callable
+ mem = memory_usage(deep=True)
return int(mem if is_scalar(mem) else mem.sum())
# no memory_usage attribute, so fall back to object's 'sizeof'
diff --git a/pandas/core/dtypes/common.py b/pandas/core/dtypes/common.py
index aa621fea6c39a..4d8d3c2816f69 100644
--- a/pandas/core/dtypes/common.py
+++ b/pandas/core/dtypes/common.py
@@ -250,7 +250,7 @@ def is_scipy_sparse(arr) -> bool:
"""
global _is_scipy_sparse
- if _is_scipy_sparse is None: # pylint: disable=used-before-assignment
+ if _is_scipy_sparse is None:
try:
from scipy.sparse import issparse as _is_scipy_sparse
except ImportError:
diff --git a/pandas/core/groupby/groupby.py b/pandas/core/groupby/groupby.py
index b313657d33b04..c91e4233ef540 100644
--- a/pandas/core/groupby/groupby.py
+++ b/pandas/core/groupby/groupby.py
@@ -5014,7 +5014,7 @@ def shift(
period = cast(int, period)
if freq is not None:
f = lambda x: x.shift(
- period, # pylint: disable=cell-var-from-loop
+ period,
freq,
0, # axis
fill_value,
diff --git a/pandas/core/indexes/multi.py b/pandas/core/indexes/multi.py
index 2e554bc848ffe..d6149bcd6fdac 100644
--- a/pandas/core/indexes/multi.py
+++ b/pandas/core/indexes/multi.py
@@ -2803,7 +2803,6 @@ def get_slice_bound(
label = (label,)
return self._partial_tup_index(label, side=side)
- # pylint: disable-next=useless-parent-delegation
def slice_locs(self, start=None, end=None, step=None) -> tuple[int, int]:
"""
For an ordered MultiIndex, compute the slice locations for input
diff --git a/pandas/core/internals/__init__.py b/pandas/core/internals/__init__.py
index 31234fb1f116f..89c8a4a27ca31 100644
--- a/pandas/core/internals/__init__.py
+++ b/pandas/core/internals/__init__.py
@@ -6,9 +6,9 @@
)
__all__ = [
- "Block", # pylint: disable=undefined-all-variable
- "DatetimeTZBlock", # pylint: disable=undefined-all-variable
- "ExtensionBlock", # pylint: disable=undefined-all-variable
+ "Block",
+ "DatetimeTZBlock",
+ "ExtensionBlock",
"make_block",
"BlockManager",
"SingleBlockManager",
diff --git a/pandas/core/series.py b/pandas/core/series.py
index 967aea15fff60..62a6178f5af4d 100644
--- a/pandas/core/series.py
+++ b/pandas/core/series.py
@@ -1469,7 +1469,6 @@ def __repr__(self) -> str:
"""
Return a string representation for a particular Series.
"""
- # pylint: disable=invalid-repr-returned
repr_params = fmt.get_series_repr_params()
return self.to_string(**repr_params)
@@ -2059,7 +2058,7 @@ def mode(self, dropna: bool = True) -> Series:
dtype=self.dtype,
).__finalize__(self, method="mode")
- def unique(self) -> ArrayLike: # pylint: disable=useless-parent-delegation
+ def unique(self) -> ArrayLike:
"""
Return unique values of Series object.
diff --git a/pandas/io/formats/style.py b/pandas/io/formats/style.py
index ab5f1c039b7ca..b2b0d711c6b54 100644
--- a/pandas/io/formats/style.py
+++ b/pandas/io/formats/style.py
@@ -3038,7 +3038,7 @@ def set_properties(self, subset: Subset | None = None, **kwargs) -> Styler:
return self.map(lambda x: values, subset=subset)
@Substitution(subset=subset_args)
- def bar( # pylint: disable=disallowed-name
+ def bar(
self,
subset: Subset | None = None,
axis: Axis | None = 0,
diff --git a/pandas/plotting/_core.py b/pandas/plotting/_core.py
index 763244c5bdf0e..60bb45d3ac1dc 100644
--- a/pandas/plotting/_core.py
+++ b/pandas/plotting/_core.py
@@ -1187,7 +1187,7 @@ def line(
)
@Substitution(kind="bar")
@Appender(_bar_or_line_doc)
- def bar( # pylint: disable=disallowed-name
+ def bar(
self,
x: Hashable | None = None,
y: Hashable | None = None,
diff --git a/pandas/plotting/_matplotlib/boxplot.py b/pandas/plotting/_matplotlib/boxplot.py
index 75b24cd42e062..2a28cd94b64e5 100644
--- a/pandas/plotting/_matplotlib/boxplot.py
+++ b/pandas/plotting/_matplotlib/boxplot.py
@@ -82,7 +82,7 @@ def __init__(self, data, return_type: str = "axes", **kwargs) -> None:
self.return_type = return_type
# Do not call LinePlot.__init__ which may fill nan
- MPLPlot.__init__(self, data, **kwargs) # pylint: disable=non-parent-init-called
+ MPLPlot.__init__(self, data, **kwargs)
if self.subplots:
# Disable label ax sharing. Otherwise, all subplots shows last
diff --git a/pandas/plotting/_matplotlib/core.py b/pandas/plotting/_matplotlib/core.py
index 700136bca8da7..38a75e741d60e 100644
--- a/pandas/plotting/_matplotlib/core.py
+++ b/pandas/plotting/_matplotlib/core.py
@@ -297,7 +297,7 @@ def __init__(
def _validate_sharex(sharex: bool | None, ax, by) -> bool:
if sharex is None:
# if by is defined, subplots are used and sharex should be False
- if ax is None and by is None: # pylint: disable=simplifiable-if-statement
+ if ax is None and by is None:
sharex = True
else:
# if we get an axis, the users should do the visibility
diff --git a/pandas/plotting/_matplotlib/hist.py b/pandas/plotting/_matplotlib/hist.py
index d9d1df128d199..ca635386be335 100644
--- a/pandas/plotting/_matplotlib/hist.py
+++ b/pandas/plotting/_matplotlib/hist.py
@@ -78,7 +78,7 @@ def __init__(
self.xlabel = kwargs.get("xlabel")
self.ylabel = kwargs.get("ylabel")
# Do not call LinePlot.__init__ which may fill nan
- MPLPlot.__init__(self, data, **kwargs) # pylint: disable=non-parent-init-called
+ MPLPlot.__init__(self, data, **kwargs)
self.bins = self._adjust_bins(bins)
@@ -236,7 +236,7 @@ def __init__(
self, data, bw_method=None, ind=None, *, weights=None, **kwargs
) -> None:
# Do not call LinePlot.__init__ which may fill nan
- MPLPlot.__init__(self, data, **kwargs) # pylint: disable=non-parent-init-called
+ MPLPlot.__init__(self, data, **kwargs)
self.bw_method = bw_method
self.ind = ind
self.weights = weights
diff --git a/pandas/tests/config/test_config.py b/pandas/tests/config/test_config.py
index 5b1d4cde9fb59..aaf6178866ecd 100644
--- a/pandas/tests/config/test_config.py
+++ b/pandas/tests/config/test_config.py
@@ -227,7 +227,6 @@ def test_validation(self):
validator = cf.is_one_of_factory([None, cf.is_callable])
cf.register_option("b", lambda: None, "doc", validator=validator)
- # pylint: disable-next=consider-using-f-string
cf.set_option("b", "%.1f".format) # Formatter is callable
cf.set_option("b", None) # Formatter is none (default)
with pytest.raises(ValueError, match="Value must be a callable"):
diff --git a/pandas/tests/dtypes/test_inference.py b/pandas/tests/dtypes/test_inference.py
index 96a67591f6c78..668e7192c0e52 100644
--- a/pandas/tests/dtypes/test_inference.py
+++ b/pandas/tests/dtypes/test_inference.py
@@ -240,8 +240,6 @@ def test_is_list_like_generic():
# is_list_like was yielding false positives for Generic classes in python 3.11
T = TypeVar("T")
- # https://github.com/pylint-dev/pylint/issues/9398
- # pylint: disable=multiple-statements
class MyDataFrame(DataFrame, Generic[T]): ...
tstc = MyDataFrame[int]
diff --git a/pandas/tests/groupby/test_grouping.py b/pandas/tests/groupby/test_grouping.py
index 9ce7a0818ac02..063b0ce38387f 100644
--- a/pandas/tests/groupby/test_grouping.py
+++ b/pandas/tests/groupby/test_grouping.py
@@ -1044,7 +1044,6 @@ def test_multi_iter_frame(self, three_group):
grouped = df.groupby(["k1", "k2"])
# calling `dict` on a DataFrameGroupBy leads to a TypeError,
# we need to use a dictionary comprehension here
- # pylint: disable-next=unnecessary-comprehension
groups = {key: gp for key, gp in grouped} # noqa: C416
assert len(groups) == 2
diff --git a/pandas/tests/indexes/datetimes/test_iter.py b/pandas/tests/indexes/datetimes/test_iter.py
index a006ed79f27ba..dbd233eed908f 100644
--- a/pandas/tests/indexes/datetimes/test_iter.py
+++ b/pandas/tests/indexes/datetimes/test_iter.py
@@ -20,7 +20,7 @@ def test_iteration_preserves_nanoseconds(self, tz):
["2018-02-08 15:00:00.168456358", "2018-02-08 15:00:00.168456359"], tz=tz
)
for i, ts in enumerate(index):
- assert ts == index[i] # pylint: disable=unnecessary-list-index-lookup
+ assert ts == index[i]
def test_iter_readonly(self):
# GH#28055 ints_to_pydatetime with readonly array
@@ -35,7 +35,7 @@ def test_iteration_preserves_tz(self):
for i, ts in enumerate(index):
result = ts
- expected = index[i] # pylint: disable=unnecessary-list-index-lookup
+ expected = index[i]
assert result == expected
def test_iteration_preserves_tz2(self):
@@ -45,7 +45,7 @@ def test_iteration_preserves_tz2(self):
for i, ts in enumerate(index):
result = ts
- expected = index[i] # pylint: disable=unnecessary-list-index-lookup
+ expected = index[i]
assert result._repr_base == expected._repr_base
assert result == expected
@@ -56,7 +56,7 @@ def test_iteration_preserves_tz3(self):
)
for i, ts in enumerate(index):
result = ts
- expected = index[i] # pylint: disable=unnecessary-list-index-lookup
+ expected = index[i]
assert result._repr_base == expected._repr_base
assert result == expected
diff --git a/pandas/tests/io/formats/style/test_html.py b/pandas/tests/io/formats/style/test_html.py
index 2306324efb974..752b9b391f9cb 100644
--- a/pandas/tests/io/formats/style/test_html.py
+++ b/pandas/tests/io/formats/style/test_html.py
@@ -821,7 +821,6 @@ def test_rendered_links(type, text, exp, found):
def test_multiple_rendered_links():
links = ("www.a.b", "http://a.c", "https://a.d", "ftp://a.e")
- # pylint: disable-next=consider-using-f-string
df = DataFrame(["text {} {} text {} {}".format(*links)])
result = df.style.format(hyperlinks="html").to_html()
href = '<a href="{0}" target="_blank">{0}</a>'
diff --git a/pandas/tests/io/test_html.py b/pandas/tests/io/test_html.py
index 2c0f19dc74ed2..f16f3a2a5c775 100644
--- a/pandas/tests/io/test_html.py
+++ b/pandas/tests/io/test_html.py
@@ -152,7 +152,6 @@ def test_to_html_compat(self, flavor_read_html):
np.random.default_rng(2).random((4, 3)),
columns=pd.Index(list("abc"), dtype=object),
)
- # pylint: disable-next=consider-using-f-string
.map("{:.3f}".format)
.astype(float)
)
@@ -1460,7 +1459,6 @@ def seek(self, offset):
def seekable(self):
return True
- # GH 49036 pylint checks for presence of __next__ for iterators
def __next__(self): ...
def __iter__(self) -> Iterator:
diff --git a/pandas/tests/reshape/test_pivot.py b/pandas/tests/reshape/test_pivot.py
index 2ccb622c7a250..97f06b0e379f4 100644
--- a/pandas/tests/reshape/test_pivot.py
+++ b/pandas/tests/reshape/test_pivot.py
@@ -819,7 +819,7 @@ def test_pivot_columns_none_raise_error(self):
df = DataFrame({"col1": ["a", "b", "c"], "col2": [1, 2, 3], "col3": [1, 2, 3]})
msg = r"pivot\(\) missing 1 required keyword-only argument: 'columns'"
with pytest.raises(TypeError, match=msg):
- df.pivot(index="col1", values="col3") # pylint: disable=missing-kwoa
+ df.pivot(index="col1", values="col3")
@pytest.mark.xfail(
reason="MultiIndexed unstack with tuple names fails with KeyError GH#19966"
@@ -2600,7 +2600,7 @@ def test_pivot_columns_not_given(self):
# GH#48293
df = DataFrame({"a": [1], "b": 1})
with pytest.raises(TypeError, match="missing 1 required keyword-only argument"):
- df.pivot() # pylint: disable=missing-kwoa
+ df.pivot()
@pytest.mark.xfail(using_pyarrow_string_dtype(), reason="None is cast to NaN")
def test_pivot_columns_is_none(self):
diff --git a/pandas/tests/scalar/timedelta/test_arithmetic.py b/pandas/tests/scalar/timedelta/test_arithmetic.py
index 96721f11cb2d6..efeca375affbb 100644
--- a/pandas/tests/scalar/timedelta/test_arithmetic.py
+++ b/pandas/tests/scalar/timedelta/test_arithmetic.py
@@ -1180,5 +1180,5 @@ def test_ops_error_str():
with pytest.raises(TypeError, match=msg):
left > right
- assert not left == right # pylint: disable=unneeded-not
+ assert not left == right
assert left != right
diff --git a/pandas/tests/scalar/timestamp/test_comparisons.py b/pandas/tests/scalar/timestamp/test_comparisons.py
index e7e5541cf499f..b2590c43e1ece 100644
--- a/pandas/tests/scalar/timestamp/test_comparisons.py
+++ b/pandas/tests/scalar/timestamp/test_comparisons.py
@@ -309,5 +309,5 @@ def __eq__(self, other) -> bool:
for left, right in [(inf, timestamp), (timestamp, inf)]:
assert left > right or left < right
assert left >= right or left <= right
- assert not left == right # pylint: disable=unneeded-not
+ assert not left == right
assert left != right
diff --git a/pandas/tests/util/test_deprecate_nonkeyword_arguments.py b/pandas/tests/util/test_deprecate_nonkeyword_arguments.py
index e74ff89b11581..f81d32b574682 100644
--- a/pandas/tests/util/test_deprecate_nonkeyword_arguments.py
+++ b/pandas/tests/util/test_deprecate_nonkeyword_arguments.py
@@ -124,8 +124,7 @@ def test_i_signature():
class Foo:
@deprecate_nonkeyword_arguments(version=None, allowed_args=["self", "bar"])
- def baz(self, bar=None, foobar=None): # pylint: disable=disallowed-name
- ...
+ def baz(self, bar=None, foobar=None): ...
def test_foo_signature():
diff --git a/pyproject.toml b/pyproject.toml
index 0a0a3e8b484f0..085c054f8241a 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -318,7 +318,61 @@ ignore = [
# pairwise-over-zipped (>=PY310 only)
"RUF007",
# mutable-class-default
- "RUF012"
+ "RUF012",
+
+ # Additional pylint rules
+ # literal-membership
+ "PLR6201", # 847 errors
+ # Method could be a function, class method, or static method
+ "PLR6301", # 11411 errors
+ # Private name import
+ "PLC2701", # 27 errors
+ # Too many positional arguments (6/5)
+ "PLR0917", # 470 errors
+ # compare-to-empty-string
+ "PLC1901",
+ # `tempfile.NamedTemporaryFile` in text mode without explicit `encoding` argument
+ "PLW1514", # 1 error
+ # Object does not implement `__hash__` method
+ "PLW1641", # 16 errors
+ # Bad or misspelled dunder method name
+ "PLW3201", # 69 errors, seems to be all false positive
+ # Unnecessary lookup of dictionary value by key
+ "PLR1733", # 5 errors, it seems like we wannt to ignore these
+ # Unnecessary lookup of list item by index
+ "PLR1736", # 4 errors, we're currently having inline pylint ignore
+ # empty-comment
+ "PLR2044", # autofixable
+ # Unpacking a dictionary in iteration without calling `.items()`
+ "PLE1141", # autofixable
+ # import-outside-toplevel
+ "PLC0415",
+ # unnecessary-dunder-call
+ "PLC2801",
+ # comparison-with-itself
+ "PLR0124",
+ # too-many-public-methods
+ "PLR0904",
+ # too-many-return-statements
+ "PLR0911",
+ # too-many-branches
+ "PLR0912",
+ # too-many-arguments
+ "PLR0913",
+ # too-many-locals
+ "PLR0914",
+ # too-many-statements
+ "PLR0915",
+ # too-many-boolean-expressions
+ "PLR0916",
+ # too-many-nested-blocks
+ "PLR1702",
+ # redefined-argument-from-local
+ "PLR1704",
+ # unnecessary-lambda
+ "PLW0108",
+ # global-statement
+ "PLW0603",
]
exclude = [
@@ -367,110 +421,6 @@ mark-parentheses = false
[tool.ruff.format]
docstring-code-format = true
-[tool.pylint.messages_control]
-max-line-length = 88
-disable = [
- # intentionally turned off
- "bad-mcs-classmethod-argument",
- "broad-except",
- "c-extension-no-member",
- "comparison-with-itself",
- "consider-using-enumerate",
- "import-error",
- "import-outside-toplevel",
- "invalid-name",
- "invalid-unary-operand-type",
- "line-too-long",
- "no-else-continue",
- "no-else-raise",
- "no-else-return",
- "no-member",
- "no-name-in-module",
- "not-an-iterable",
- "overridden-final-method",
- "pointless-statement",
- "redundant-keyword-arg",
- "singleton-comparison",
- "too-many-ancestors",
- "too-many-arguments",
- "too-many-boolean-expressions",
- "too-many-branches",
- "too-many-function-args",
- "too-many-instance-attributes",
- "too-many-locals",
- "too-many-nested-blocks",
- "too-many-public-methods",
- "too-many-return-statements",
- "too-many-statements",
- "unexpected-keyword-arg",
- "ungrouped-imports",
- "unsubscriptable-object",
- "unsupported-assignment-operation",
- "unsupported-membership-test",
- "unused-import",
- "use-dict-literal",
- "use-implicit-booleaness-not-comparison",
- "use-implicit-booleaness-not-len",
- "wrong-import-order",
- "wrong-import-position",
- "redefined-loop-name",
-
- # misc
- "abstract-class-instantiated",
- "no-value-for-parameter",
- "undefined-variable",
- "unpacking-non-sequence",
- "used-before-assignment",
-
- # pylint type "C": convention, for programming standard violation
- "missing-class-docstring",
- "missing-function-docstring",
- "missing-module-docstring",
- "superfluous-parens",
- "too-many-lines",
- "unidiomatic-typecheck",
- "unnecessary-dunder-call",
- "unnecessary-lambda-assignment",
-
- # pylint type "R": refactor, for bad code smell
- "consider-using-with",
- "cyclic-import",
- "duplicate-code",
- "inconsistent-return-statements",
- "redefined-argument-from-local",
- "too-few-public-methods",
-
- # pylint type "W": warning, for python specific problems
- "abstract-method",
- "arguments-differ",
- "arguments-out-of-order",
- "arguments-renamed",
- "attribute-defined-outside-init",
- "broad-exception-raised",
- "comparison-with-callable",
- "dangerous-default-value",
- "deprecated-module",
- "eval-used",
- "expression-not-assigned",
- "fixme",
- "global-statement",
- "invalid-overridden-method",
- "keyword-arg-before-vararg",
- "possibly-unused-variable",
- "protected-access",
- "raise-missing-from",
- "redefined-builtin",
- "redefined-outer-name",
- "self-cls-assignment",
- "signature-differs",
- "super-init-not-called",
- "try-except-raise",
- "unnecessary-lambda",
- "unused-argument",
- "unused-variable",
- "using-constant-test"
-]
-
[tool.pytest.ini_options]
# sync minversion with pyproject.toml & install.rst
minversion = "7.3.2"
diff --git a/scripts/tests/data/deps_minimum.toml b/scripts/tests/data/deps_minimum.toml
index ca1dc0c961c42..ed7b9affe9a50 100644
--- a/scripts/tests/data/deps_minimum.toml
+++ b/scripts/tests/data/deps_minimum.toml
@@ -231,104 +231,6 @@ exclude = [
"env",
]
-[tool.pylint.messages_control]
-max-line-length = 88
-disable = [
- # intentionally turned off
- "broad-except",
- "c-extension-no-member",
- "comparison-with-itself",
- "import-error",
- "import-outside-toplevel",
- "invalid-name",
- "invalid-unary-operand-type",
- "line-too-long",
- "no-else-continue",
- "no-else-raise",
- "no-else-return",
- "no-member",
- "no-name-in-module",
- "not-an-iterable",
- "overridden-final-method",
- "pointless-statement",
- "redundant-keyword-arg",
- "singleton-comparison",
- "too-many-ancestors",
- "too-many-arguments",
- "too-many-boolean-expressions",
- "too-many-branches",
- "too-many-function-args",
- "too-many-instance-attributes",
- "too-many-locals",
- "too-many-nested-blocks",
- "too-many-public-methods",
- "too-many-return-statements",
- "too-many-statements",
- "unexpected-keyword-arg",
- "ungrouped-imports",
- "unsubscriptable-object",
- "unsupported-assignment-operation",
- "unsupported-membership-test",
- "unused-import",
- "use-implicit-booleaness-not-comparison",
- "use-implicit-booleaness-not-len",
- "wrong-import-order",
- "wrong-import-position",
-
- # misc
- "abstract-class-instantiated",
- "no-value-for-parameter",
- "undefined-variable",
- "unpacking-non-sequence",
-
- # pylint type "C": convention, for programming standard violation
- "missing-class-docstring",
- "missing-function-docstring",
- "missing-module-docstring",
- "too-many-lines",
- "unidiomatic-typecheck",
- "unnecessary-dunder-call",
- "unnecessary-lambda-assignment",
-
- # pylint type "R": refactor, for bad code smell
- "consider-using-with",
- "cyclic-import",
- "duplicate-code",
- "inconsistent-return-statements",
- "redefined-argument-from-local",
- "too-few-public-methods",
-
- # pylint type "W": warning, for python specific problems
- "abstract-method",
- "arguments-differ",
- "arguments-out-of-order",
- "arguments-renamed",
- "attribute-defined-outside-init",
- "comparison-with-callable",
- "dangerous-default-value",
- "deprecated-module",
- "eval-used",
- "expression-not-assigned",
- "fixme",
- "global-statement",
- "invalid-overridden-method",
- "keyword-arg-before-vararg",
- "possibly-unused-variable",
- "protected-access",
- "raise-missing-from",
- "redefined-builtin",
- "redefined-outer-name",
- "self-cls-assignment",
- "signature-differs",
- "super-init-not-called",
- "try-except-raise",
- "unnecessary-lambda",
- "unspecified-encoding",
- "unused-argument",
- "unused-variable",
- "using-constant-test"
-]
-
[tool.pytest.ini_options]
# sync minversion with pyproject.toml & install.rst
minversion = "7.0"
| Hi @mroeschke, while looking into the `pylint` rule, I noticed that most of them are currently gated behind the `preview` flag. Do you think we should enable the `preview` flag or wait some rules to be promoted to stable in `ruff==0.2.0` (should be released in the next few weeks)
Other than that, this PR is ready for review.
- [ ] closes #xxxx (Replace xxxx with the GitHub issue number)
- [ ] [Tests added and passed](https://pandas.pydata.org/pandas-docs/dev/development/contributing_codebase.html#writing-tests) if fixing a bug or adding a new feature
- [ ] All [code checks passed](https://pandas.pydata.org/pandas-docs/dev/development/contributing_codebase.html#pre-commit).
- [ ] Added [type annotations](https://pandas.pydata.org/pandas-docs/dev/development/contributing_codebase.html#type-hints) to new arguments/methods/functions.
- [ ] Added an entry in the latest `doc/source/whatsnew/vX.X.X.rst` file if fixing a bug or adding a new feature.
| https://api.github.com/repos/pandas-dev/pandas/pulls/57182 | 2024-01-31T20:03:19Z | 2024-04-08T21:24:19Z | 2024-04-08T21:24:19Z | 2024-04-09T21:15:39Z |
Backport PR #57169 on branch 2.2.x (REGR: DataFrame.sort_index not producing stable sort) | diff --git a/doc/source/whatsnew/v2.2.1.rst b/doc/source/whatsnew/v2.2.1.rst
index 19b7e3493f964..713e3787825fb 100644
--- a/doc/source/whatsnew/v2.2.1.rst
+++ b/doc/source/whatsnew/v2.2.1.rst
@@ -18,6 +18,7 @@ Fixed regressions
- Fixed regression in :func:`merge_ordered` raising ``TypeError`` for ``fill_method="ffill"`` and ``how="left"`` (:issue:`57010`)
- Fixed regression in :func:`wide_to_long` raising an ``AttributeError`` for string columns (:issue:`57066`)
- Fixed regression in :meth:`DataFrame.loc` raising ``IndexError`` for non-unique, masked dtype indexes where result has more than 10,000 rows (:issue:`57027`)
+- Fixed regression in :meth:`DataFrame.sort_index` not producing a stable sort for a index with duplicates (:issue:`57151`)
- Fixed regression in :meth:`DataFrame.to_dict` with ``orient='list'`` and datetime or timedelta types returning integers (:issue:`54824`)
- Fixed regression in :meth:`DataFrameGroupBy.idxmin`, :meth:`DataFrameGroupBy.idxmax`, :meth:`SeriesGroupBy.idxmin`, :meth:`SeriesGroupBy.idxmax` ignoring the ``skipna`` argument (:issue:`57040`)
- Fixed regression in :meth:`DataFrameGroupBy.idxmin`, :meth:`DataFrameGroupBy.idxmax`, :meth:`SeriesGroupBy.idxmin`, :meth:`SeriesGroupBy.idxmax` where values containing the minimum or maximum value for the dtype could produce incorrect results (:issue:`57040`)
diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py
index 5e7f2e27f1275..6f9078bf5a2a2 100644
--- a/pandas/core/indexes/base.py
+++ b/pandas/core/indexes/base.py
@@ -5914,17 +5914,14 @@ def sort_values(
(Index([1000, 100, 10, 1], dtype='int64'), array([3, 1, 0, 2]))
"""
if key is None and (
- self.is_monotonic_increasing or self.is_monotonic_decreasing
+ (ascending and self.is_monotonic_increasing)
+ or (not ascending and self.is_monotonic_decreasing)
):
- reverse = ascending != self.is_monotonic_increasing
- sorted_index = self[::-1] if reverse else self.copy()
if return_indexer:
indexer = np.arange(len(self), dtype=np.intp)
- if reverse:
- indexer = indexer[::-1]
- return sorted_index, indexer
+ return self.copy(), indexer
else:
- return sorted_index
+ return self.copy()
# GH 35584. Sort missing values according to na_position kwarg
# ignore na_position for MultiIndex
diff --git a/pandas/tests/frame/methods/test_sort_index.py b/pandas/tests/frame/methods/test_sort_index.py
index 49e292057e4dc..830561a1349ee 100644
--- a/pandas/tests/frame/methods/test_sort_index.py
+++ b/pandas/tests/frame/methods/test_sort_index.py
@@ -1002,3 +1002,27 @@ def test_axis_columns_ignore_index():
result = df.sort_index(axis="columns", ignore_index=True)
expected = DataFrame([[2, 1]])
tm.assert_frame_equal(result, expected)
+
+
+def test_sort_index_stable_sort():
+ # GH 57151
+ df = DataFrame(
+ data=[
+ (Timestamp("2024-01-30 13:00:00"), 13.0),
+ (Timestamp("2024-01-30 13:00:00"), 13.1),
+ (Timestamp("2024-01-30 12:00:00"), 12.0),
+ (Timestamp("2024-01-30 12:00:00"), 12.1),
+ ],
+ columns=["dt", "value"],
+ ).set_index(["dt"])
+ result = df.sort_index(level="dt", kind="stable")
+ expected = DataFrame(
+ data=[
+ (Timestamp("2024-01-30 12:00:00"), 12.0),
+ (Timestamp("2024-01-30 12:00:00"), 12.1),
+ (Timestamp("2024-01-30 13:00:00"), 13.0),
+ (Timestamp("2024-01-30 13:00:00"), 13.1),
+ ],
+ columns=["dt", "value"],
+ ).set_index(["dt"])
+ tm.assert_frame_equal(result, expected)
| Backport PR #57169: REGR: DataFrame.sort_index not producing stable sort | https://api.github.com/repos/pandas-dev/pandas/pulls/57180 | 2024-01-31T18:20:19Z | 2024-01-31T19:20:42Z | 2024-01-31T19:20:42Z | 2024-01-31T19:20:42Z |
Backport PR #57175 on branch 2.2.x (BUG: Interchange protocol implementation handles empty dataframes incorrectly) | diff --git a/doc/source/whatsnew/v2.2.1.rst b/doc/source/whatsnew/v2.2.1.rst
index 19b7e3493f964..4a20eda08937a 100644
--- a/doc/source/whatsnew/v2.2.1.rst
+++ b/doc/source/whatsnew/v2.2.1.rst
@@ -29,6 +29,7 @@ Fixed regressions
Bug fixes
~~~~~~~~~
+- Fixed bug in :func:`pandas.api.interchange.from_dataframe` which was raising for empty inputs (:issue:`56700`)
- Fixed bug in :meth:`DataFrame.__getitem__` for empty :class:`DataFrame` with Copy-on-Write enabled (:issue:`57130`)
.. ---------------------------------------------------------------------------
diff --git a/pandas/core/interchange/buffer.py b/pandas/core/interchange/buffer.py
index a54e4428bd836..5c97fc17d7070 100644
--- a/pandas/core/interchange/buffer.py
+++ b/pandas/core/interchange/buffer.py
@@ -23,7 +23,7 @@ def __init__(self, x: np.ndarray, allow_copy: bool = True) -> None:
"""
Handle only regular columns (= numpy arrays) for now.
"""
- if not x.strides == (x.dtype.itemsize,):
+ if x.strides[0] and not x.strides == (x.dtype.itemsize,):
# The protocol does not support strided buffers, so a copy is
# necessary. If that's not allowed, we need to raise an exception.
if allow_copy:
diff --git a/pandas/tests/interchange/test_impl.py b/pandas/tests/interchange/test_impl.py
index c7b13f9fd7b2d..c6365233728d2 100644
--- a/pandas/tests/interchange/test_impl.py
+++ b/pandas/tests/interchange/test_impl.py
@@ -379,3 +379,12 @@ def test_large_string():
result = pd.api.interchange.from_dataframe(df.__dataframe__())
expected = pd.DataFrame({"a": ["x"]}, dtype="object")
tm.assert_frame_equal(result, expected)
+
+
+def test_empty_dataframe():
+ # https://github.com/pandas-dev/pandas/issues/56700
+ df = pd.DataFrame({"a": []}, dtype="int8")
+ dfi = df.__dataframe__()
+ result = pd.api.interchange.from_dataframe(dfi, allow_copy=False)
+ expected = pd.DataFrame({"a": []}, dtype="int8")
+ tm.assert_frame_equal(result, expected)
| Backport PR #57175: BUG: Interchange protocol implementation handles empty dataframes incorrectly | https://api.github.com/repos/pandas-dev/pandas/pulls/57179 | 2024-01-31T18:05:45Z | 2024-01-31T19:06:36Z | 2024-01-31T19:06:36Z | 2024-01-31T19:06:36Z |
BUG: Interchange protocol implementation handles empty dataframes incorrectly | diff --git a/doc/source/whatsnew/v2.2.1.rst b/doc/source/whatsnew/v2.2.1.rst
index 19b7e3493f964..4a20eda08937a 100644
--- a/doc/source/whatsnew/v2.2.1.rst
+++ b/doc/source/whatsnew/v2.2.1.rst
@@ -29,6 +29,7 @@ Fixed regressions
Bug fixes
~~~~~~~~~
+- Fixed bug in :func:`pandas.api.interchange.from_dataframe` which was raising for empty inputs (:issue:`56700`)
- Fixed bug in :meth:`DataFrame.__getitem__` for empty :class:`DataFrame` with Copy-on-Write enabled (:issue:`57130`)
.. ---------------------------------------------------------------------------
diff --git a/pandas/core/interchange/buffer.py b/pandas/core/interchange/buffer.py
index a54e4428bd836..5c97fc17d7070 100644
--- a/pandas/core/interchange/buffer.py
+++ b/pandas/core/interchange/buffer.py
@@ -23,7 +23,7 @@ def __init__(self, x: np.ndarray, allow_copy: bool = True) -> None:
"""
Handle only regular columns (= numpy arrays) for now.
"""
- if not x.strides == (x.dtype.itemsize,):
+ if x.strides[0] and not x.strides == (x.dtype.itemsize,):
# The protocol does not support strided buffers, so a copy is
# necessary. If that's not allowed, we need to raise an exception.
if allow_copy:
diff --git a/pandas/tests/interchange/test_impl.py b/pandas/tests/interchange/test_impl.py
index aeee98f5a125e..76f80c20fa217 100644
--- a/pandas/tests/interchange/test_impl.py
+++ b/pandas/tests/interchange/test_impl.py
@@ -392,3 +392,12 @@ def test_large_string():
result = pd.api.interchange.from_dataframe(df.__dataframe__())
expected = pd.DataFrame({"a": ["x"]}, dtype="object")
tm.assert_frame_equal(result, expected)
+
+
+def test_empty_dataframe():
+ # https://github.com/pandas-dev/pandas/issues/56700
+ df = pd.DataFrame({"a": []}, dtype="int8")
+ dfi = df.__dataframe__()
+ result = pd.api.interchange.from_dataframe(dfi, allow_copy=False)
+ expected = pd.DataFrame({"a": []}, dtype="int8")
+ tm.assert_frame_equal(result, expected)
| - [ ] closes #56700 (Replace xxxx with the GitHub issue number)
- [ ] [Tests added and passed](https://pandas.pydata.org/pandas-docs/dev/development/contributing_codebase.html#writing-tests) if fixing a bug or adding a new feature
- [ ] All [code checks passed](https://pandas.pydata.org/pandas-docs/dev/development/contributing_codebase.html#pre-commit).
- [ ] Added [type annotations](https://pandas.pydata.org/pandas-docs/dev/development/contributing_codebase.html#type-hints) to new arguments/methods/functions.
- [ ] Added an entry in the latest `doc/source/whatsnew/vX.X.X.rst` file if fixing a bug or adding a new feature.
| https://api.github.com/repos/pandas-dev/pandas/pulls/57175 | 2024-01-31T14:31:00Z | 2024-01-31T18:05:14Z | 2024-01-31T18:05:14Z | 2024-01-31T18:05:22Z |
BUG: Interchange protocol implementation allows non-string column names | diff --git a/doc/source/whatsnew/v2.2.1.rst b/doc/source/whatsnew/v2.2.1.rst
index 56e645d4c55db..13d5024b5a131 100644
--- a/doc/source/whatsnew/v2.2.1.rst
+++ b/doc/source/whatsnew/v2.2.1.rst
@@ -32,6 +32,7 @@ Bug fixes
~~~~~~~~~
- Fixed bug in :func:`pandas.api.interchange.from_dataframe` which was raising for Nullable integers (:issue:`55069`)
- Fixed bug in :func:`pandas.api.interchange.from_dataframe` which was raising for empty inputs (:issue:`56700`)
+- Fixed bug in :func:`pandas.api.interchange.from_dataframe` which wasn't converting columns names to strings (:issue:`55069`)
- Fixed bug in :meth:`DataFrame.__getitem__` for empty :class:`DataFrame` with Copy-on-Write enabled (:issue:`57130`)
.. ---------------------------------------------------------------------------
diff --git a/pandas/core/interchange/column.py b/pandas/core/interchange/column.py
index 350cab2c56013..2e60b2a2138e3 100644
--- a/pandas/core/interchange/column.py
+++ b/pandas/core/interchange/column.py
@@ -77,6 +77,14 @@ def __init__(self, column: pd.Series, allow_copy: bool = True) -> None:
Note: doesn't deal with extension arrays yet, just assume a regular
Series/ndarray for now.
"""
+ if isinstance(column, pd.DataFrame):
+ raise TypeError(
+ "Expected a Series, got a DataFrame. This likely happened "
+ "because you called __dataframe__ on a DataFrame which, "
+ "after converting column names to string, resulted in duplicated "
+ f"names: {column.columns}. Please rename these columns before "
+ "using the interchange protocol."
+ )
if not isinstance(column, pd.Series):
raise NotImplementedError(f"Columns of type {type(column)} not handled yet")
diff --git a/pandas/core/interchange/dataframe.py b/pandas/core/interchange/dataframe.py
index 4f08b2c2b3a7b..1ffe0e8e8dbb0 100644
--- a/pandas/core/interchange/dataframe.py
+++ b/pandas/core/interchange/dataframe.py
@@ -32,7 +32,7 @@ def __init__(self, df: DataFrame, allow_copy: bool = True) -> None:
Constructor - an instance of this (private) class is returned from
`pd.DataFrame.__dataframe__`.
"""
- self._df = df
+ self._df = df.rename(columns=str, copy=False)
self._allow_copy = allow_copy
def __dataframe__(
diff --git a/pandas/tests/interchange/test_impl.py b/pandas/tests/interchange/test_impl.py
index c8f286f22a43e..4933cca97462f 100644
--- a/pandas/tests/interchange/test_impl.py
+++ b/pandas/tests/interchange/test_impl.py
@@ -162,8 +162,6 @@ def test_missing_from_masked():
}
)
- df2 = df.__dataframe__()
-
rng = np.random.default_rng(2)
dict_null = {col: rng.integers(low=0, high=len(df)) for col in df.columns}
for col, num_nulls in dict_null.items():
@@ -395,6 +393,30 @@ def test_large_string():
tm.assert_frame_equal(result, expected)
+def test_non_str_names():
+ # https://github.com/pandas-dev/pandas/issues/56701
+ df = pd.Series([1, 2, 3], name=0).to_frame()
+ names = df.__dataframe__().column_names()
+ assert names == ["0"]
+
+
+def test_non_str_names_w_duplicates():
+ # https://github.com/pandas-dev/pandas/issues/56701
+ df = pd.DataFrame({"0": [1, 2, 3], 0: [4, 5, 6]})
+ dfi = df.__dataframe__()
+ with pytest.raises(
+ TypeError,
+ match=(
+ "Expected a Series, got a DataFrame. This likely happened because you "
+ "called __dataframe__ on a DataFrame which, after converting column "
+ r"names to string, resulted in duplicated names: Index\(\['0', '0'\], "
+ r"dtype='object'\). Please rename these columns before using the "
+ "interchange protocol."
+ ),
+ ):
+ pd.api.interchange.from_dataframe(dfi, allow_copy=False)
+
+
@pytest.mark.parametrize(
"dtype", ["Int8", pytest.param("Int8[pyarrow]", marks=td.skip_if_no("pyarrow"))]
)
| - [ ] closes #56701 (Replace xxxx with the GitHub issue number)
- [ ] [Tests added and passed](https://pandas.pydata.org/pandas-docs/dev/development/contributing_codebase.html#writing-tests) if fixing a bug or adding a new feature
- [ ] All [code checks passed](https://pandas.pydata.org/pandas-docs/dev/development/contributing_codebase.html#pre-commit).
- [ ] Added [type annotations](https://pandas.pydata.org/pandas-docs/dev/development/contributing_codebase.html#type-hints) to new arguments/methods/functions.
- [ ] Added an entry in the latest `doc/source/whatsnew/vX.X.X.rst` file if fixing a bug or adding a new feature.
| https://api.github.com/repos/pandas-dev/pandas/pulls/57174 | 2024-01-31T14:22:15Z | 2024-02-02T01:25:43Z | 2024-02-02T01:25:43Z | 2024-02-02T01:26:35Z |
BUG: pandas int extension dtypes has no attribute byteorder | diff --git a/doc/source/whatsnew/v2.2.1.rst b/doc/source/whatsnew/v2.2.1.rst
index 9002d9af2c602..56e645d4c55db 100644
--- a/doc/source/whatsnew/v2.2.1.rst
+++ b/doc/source/whatsnew/v2.2.1.rst
@@ -30,6 +30,7 @@ Fixed regressions
Bug fixes
~~~~~~~~~
+- Fixed bug in :func:`pandas.api.interchange.from_dataframe` which was raising for Nullable integers (:issue:`55069`)
- Fixed bug in :func:`pandas.api.interchange.from_dataframe` which was raising for empty inputs (:issue:`56700`)
- Fixed bug in :meth:`DataFrame.__getitem__` for empty :class:`DataFrame` with Copy-on-Write enabled (:issue:`57130`)
diff --git a/pandas/core/interchange/column.py b/pandas/core/interchange/column.py
index 508cd74c57288..350cab2c56013 100644
--- a/pandas/core/interchange/column.py
+++ b/pandas/core/interchange/column.py
@@ -11,6 +11,7 @@
from pandas.core.dtypes.dtypes import (
ArrowDtype,
+ BaseMaskedDtype,
DatetimeTZDtype,
)
@@ -143,6 +144,8 @@ def _dtype_from_pandasdtype(self, dtype) -> tuple[DtypeKind, int, str, str]:
byteorder = dtype.numpy_dtype.byteorder
elif isinstance(dtype, DatetimeTZDtype):
byteorder = dtype.base.byteorder # type: ignore[union-attr]
+ elif isinstance(dtype, BaseMaskedDtype):
+ byteorder = dtype.numpy_dtype.byteorder
else:
byteorder = dtype.byteorder
diff --git a/pandas/tests/interchange/test_impl.py b/pandas/tests/interchange/test_impl.py
index 76f80c20fa217..c8f286f22a43e 100644
--- a/pandas/tests/interchange/test_impl.py
+++ b/pandas/tests/interchange/test_impl.py
@@ -8,6 +8,7 @@
is_ci_environment,
is_platform_windows,
)
+import pandas.util._test_decorators as td
import pandas as pd
import pandas._testing as tm
@@ -394,6 +395,17 @@ def test_large_string():
tm.assert_frame_equal(result, expected)
+@pytest.mark.parametrize(
+ "dtype", ["Int8", pytest.param("Int8[pyarrow]", marks=td.skip_if_no("pyarrow"))]
+)
+def test_nullable_integers(dtype: str) -> None:
+ # https://github.com/pandas-dev/pandas/issues/55069
+ df = pd.DataFrame({"a": [1]}, dtype=dtype)
+ expected = pd.DataFrame({"a": [1]}, dtype="int8")
+ result = pd.api.interchange.from_dataframe(df.__dataframe__())
+ tm.assert_frame_equal(result, expected)
+
+
def test_empty_dataframe():
# https://github.com/pandas-dev/pandas/issues/56700
df = pd.DataFrame({"a": []}, dtype="int8")
| - [ ] closes #55069 (Replace xxxx with the GitHub issue number)
- [ ] [Tests added and passed](https://pandas.pydata.org/pandas-docs/dev/development/contributing_codebase.html#writing-tests) if fixing a bug or adding a new feature
- [ ] All [code checks passed](https://pandas.pydata.org/pandas-docs/dev/development/contributing_codebase.html#pre-commit).
- [ ] Added [type annotations](https://pandas.pydata.org/pandas-docs/dev/development/contributing_codebase.html#type-hints) to new arguments/methods/functions.
- [ ] Added an entry in the latest `doc/source/whatsnew/vX.X.X.rst` file if fixing a bug or adding a new feature.
| https://api.github.com/repos/pandas-dev/pandas/pulls/57173 | 2024-01-31T14:04:12Z | 2024-02-01T19:04:29Z | 2024-02-01T19:04:28Z | 2024-03-01T13:54:29Z |
MAINT: Adjust the codebase to the new `np.array`'s `copy` keyword meaning | diff --git a/pandas/core/array_algos/quantile.py b/pandas/core/array_algos/quantile.py
index ee6f00b219a15..5c933294fb944 100644
--- a/pandas/core/array_algos/quantile.py
+++ b/pandas/core/array_algos/quantile.py
@@ -102,7 +102,7 @@ def quantile_with_mask(
interpolation=interpolation,
)
- result = np.array(result, copy=False)
+ result = np.asarray(result)
result = result.T
return result
@@ -201,9 +201,9 @@ def _nanpercentile(
]
if values.dtype.kind == "f":
# preserve itemsize
- result = np.array(result, dtype=values.dtype, copy=False).T
+ result = np.asarray(result, dtype=values.dtype).T
else:
- result = np.array(result, copy=False).T
+ result = np.asarray(result).T
if (
result.dtype != values.dtype
and not mask.all()
diff --git a/pandas/core/arrays/arrow/array.py b/pandas/core/arrays/arrow/array.py
index f4284cb0d0e5e..cddccd7b45a3e 100644
--- a/pandas/core/arrays/arrow/array.py
+++ b/pandas/core/arrays/arrow/array.py
@@ -659,7 +659,9 @@ def __arrow_array__(self, type=None):
"""Convert myself to a pyarrow ChunkedArray."""
return self._pa_array
- def __array__(self, dtype: NpDtype | None = None) -> np.ndarray:
+ def __array__(
+ self, dtype: NpDtype | None = None, copy: bool | None = None
+ ) -> np.ndarray:
"""Correctly construct numpy arrays when passed to `np.asarray()`."""
return self.to_numpy(dtype=dtype)
diff --git a/pandas/core/arrays/base.py b/pandas/core/arrays/base.py
index 33e853ea16374..a0da3518f8e5e 100644
--- a/pandas/core/arrays/base.py
+++ b/pandas/core/arrays/base.py
@@ -725,7 +725,10 @@ def astype(self, dtype: AstypeArg, copy: bool = True) -> ArrayLike:
return TimedeltaArray._from_sequence(self, dtype=dtype, copy=copy)
- return np.array(self, dtype=dtype, copy=copy)
+ if not copy:
+ return np.asarray(self, dtype=dtype)
+ else:
+ return np.array(self, dtype=dtype, copy=copy)
def isna(self) -> np.ndarray | ExtensionArraySupportsAnyAll:
"""
diff --git a/pandas/core/arrays/categorical.py b/pandas/core/arrays/categorical.py
index 49b8ba4c47811..f37513b2bc8fd 100644
--- a/pandas/core/arrays/categorical.py
+++ b/pandas/core/arrays/categorical.py
@@ -1659,7 +1659,9 @@ def _validate_codes_for_dtype(cls, codes, *, dtype: CategoricalDtype) -> np.ndar
# -------------------------------------------------------------
@ravel_compat
- def __array__(self, dtype: NpDtype | None = None) -> np.ndarray:
+ def __array__(
+ self, dtype: NpDtype | None = None, copy: bool | None = None
+ ) -> np.ndarray:
"""
The numpy array interface.
@@ -1668,6 +1670,9 @@ def __array__(self, dtype: NpDtype | None = None) -> np.ndarray:
dtype : np.dtype or None
Specifies the the dtype for the array.
+ copy : bool or None, optional
+ Unused.
+
Returns
-------
numpy.array
diff --git a/pandas/core/arrays/datetimelike.py b/pandas/core/arrays/datetimelike.py
index 1805a86ee32ce..3f46c2896a28a 100644
--- a/pandas/core/arrays/datetimelike.py
+++ b/pandas/core/arrays/datetimelike.py
@@ -353,7 +353,9 @@ def _formatter(self, boxed: bool = False) -> Callable[[object], str]:
# ----------------------------------------------------------------
# Array-Like / EA-Interface Methods
- def __array__(self, dtype: NpDtype | None = None) -> np.ndarray:
+ def __array__(
+ self, dtype: NpDtype | None = None, copy: bool | None = None
+ ) -> np.ndarray:
# used for Timedelta/DatetimeArray, overwritten by PeriodArray
if is_object_dtype(dtype):
return np.array(list(self), dtype=object)
diff --git a/pandas/core/arrays/datetimes.py b/pandas/core/arrays/datetimes.py
index b2e3388be7b03..11516692801a1 100644
--- a/pandas/core/arrays/datetimes.py
+++ b/pandas/core/arrays/datetimes.py
@@ -649,12 +649,12 @@ def _resolution_obj(self) -> Resolution:
# ----------------------------------------------------------------
# Array-Like / EA-Interface Methods
- def __array__(self, dtype=None) -> np.ndarray:
+ def __array__(self, dtype=None, copy=None) -> np.ndarray:
if dtype is None and self.tz:
# The default for tz-aware is object, to preserve tz info
dtype = object
- return super().__array__(dtype=dtype)
+ return super().__array__(dtype=dtype, copy=copy)
def __iter__(self) -> Iterator:
"""
@@ -2421,7 +2421,7 @@ def objects_to_datetime64(
assert errors in ["raise", "coerce"]
# if str-dtype, convert
- data = np.array(data, copy=False, dtype=np.object_)
+ data = np.asarray(data, dtype=np.object_)
result, tz_parsed = tslib.array_to_datetime(
data,
diff --git a/pandas/core/arrays/interval.py b/pandas/core/arrays/interval.py
index 05e8b981f4e8a..5e7e7e949169b 100644
--- a/pandas/core/arrays/interval.py
+++ b/pandas/core/arrays/interval.py
@@ -1564,7 +1564,9 @@ def is_non_overlapping_monotonic(self) -> bool:
# ---------------------------------------------------------------------
# Conversion
- def __array__(self, dtype: NpDtype | None = None) -> np.ndarray:
+ def __array__(
+ self, dtype: NpDtype | None = None, copy: bool | None = None
+ ) -> np.ndarray:
"""
Return the IntervalArray's data as a numpy array of Interval
objects (with dtype='object')
diff --git a/pandas/core/arrays/masked.py b/pandas/core/arrays/masked.py
index c336706da45d6..cf9ba3c3dbad5 100644
--- a/pandas/core/arrays/masked.py
+++ b/pandas/core/arrays/masked.py
@@ -594,7 +594,9 @@ def astype(self, dtype: AstypeArg, copy: bool = True) -> ArrayLike:
__array_priority__ = 1000 # higher than ndarray so ops dispatch to us
- def __array__(self, dtype: NpDtype | None = None) -> np.ndarray:
+ def __array__(
+ self, dtype: NpDtype | None = None, copy: bool | None = None
+ ) -> np.ndarray:
"""
the array interface, return my values
We return an object array here to preserve our scalar values
diff --git a/pandas/core/arrays/numeric.py b/pandas/core/arrays/numeric.py
index b946356a7f8ce..fe7b32ec9652e 100644
--- a/pandas/core/arrays/numeric.py
+++ b/pandas/core/arrays/numeric.py
@@ -160,7 +160,10 @@ def _coerce_to_data_and_mask(
return values, mask, dtype, inferred_type
original = values
- values = np.array(values, copy=copy)
+ if not copy:
+ values = np.asarray(values)
+ else:
+ values = np.array(values, copy=copy)
inferred_type = None
if values.dtype == object or is_string_dtype(values.dtype):
inferred_type = lib.infer_dtype(values, skipna=True)
@@ -169,7 +172,10 @@ def _coerce_to_data_and_mask(
raise TypeError(f"{values.dtype} cannot be converted to {name}")
elif values.dtype.kind == "b" and checker(dtype):
- values = np.array(values, dtype=default_dtype, copy=copy)
+ if not copy:
+ values = np.asarray(values, dtype=default_dtype)
+ else:
+ values = np.array(values, dtype=default_dtype, copy=copy)
elif values.dtype.kind not in "iuf":
name = dtype_cls.__name__.strip("_")
@@ -208,9 +214,9 @@ def _coerce_to_data_and_mask(
inferred_type not in ["floating", "mixed-integer-float"]
and not mask.any()
):
- values = np.array(original, dtype=dtype, copy=False)
+ values = np.asarray(original, dtype=dtype)
else:
- values = np.array(original, dtype="object", copy=False)
+ values = np.asarray(original, dtype="object")
# we copy as need to coerce here
if mask.any():
diff --git a/pandas/core/arrays/numpy_.py b/pandas/core/arrays/numpy_.py
index d83a37088daec..07eb91e0cb13b 100644
--- a/pandas/core/arrays/numpy_.py
+++ b/pandas/core/arrays/numpy_.py
@@ -150,7 +150,9 @@ def dtype(self) -> NumpyEADtype:
# ------------------------------------------------------------------------
# NumPy Array Interface
- def __array__(self, dtype: NpDtype | None = None) -> np.ndarray:
+ def __array__(
+ self, dtype: NpDtype | None = None, copy: bool | None = None
+ ) -> np.ndarray:
return np.asarray(self._ndarray, dtype=dtype)
def __array_ufunc__(self, ufunc: np.ufunc, method: str, *inputs, **kwargs):
diff --git a/pandas/core/arrays/period.py b/pandas/core/arrays/period.py
index 640f6669e21eb..73cc8e4345d3c 100644
--- a/pandas/core/arrays/period.py
+++ b/pandas/core/arrays/period.py
@@ -256,7 +256,10 @@ def __init__(
raise raise_on_incompatible(values, dtype.freq)
values, dtype = values._ndarray, values.dtype
- values = np.array(values, dtype="int64", copy=copy)
+ if not copy:
+ values = np.asarray(values, dtype="int64")
+ else:
+ values = np.array(values, dtype="int64", copy=copy)
if dtype is None:
raise ValueError("dtype is not specified and cannot be inferred")
dtype = cast(PeriodDtype, dtype)
@@ -400,7 +403,9 @@ def freq(self) -> BaseOffset:
def freqstr(self) -> str:
return PeriodDtype(self.freq)._freqstr
- def __array__(self, dtype: NpDtype | None = None) -> np.ndarray:
+ def __array__(
+ self, dtype: NpDtype | None = None, copy: bool | None = None
+ ) -> np.ndarray:
if dtype == "i8":
return self.asi8
elif dtype == bool:
diff --git a/pandas/core/arrays/sparse/array.py b/pandas/core/arrays/sparse/array.py
index 05e8c968e46d8..48147f10ba4b7 100644
--- a/pandas/core/arrays/sparse/array.py
+++ b/pandas/core/arrays/sparse/array.py
@@ -554,7 +554,9 @@ def from_spmatrix(cls, data: spmatrix) -> Self:
return cls._simple_new(arr, index, dtype)
- def __array__(self, dtype: NpDtype | None = None) -> np.ndarray:
+ def __array__(
+ self, dtype: NpDtype | None = None, copy: bool | None = None
+ ) -> np.ndarray:
fill_value = self.fill_value
if self.sp_index.ngaps == 0:
diff --git a/pandas/core/arrays/timedeltas.py b/pandas/core/arrays/timedeltas.py
index 51075939276f7..c41e078095feb 100644
--- a/pandas/core/arrays/timedeltas.py
+++ b/pandas/core/arrays/timedeltas.py
@@ -1072,7 +1072,10 @@ def sequence_to_td64ns(
# This includes datetime64-dtype, see GH#23539, GH#29794
raise TypeError(f"dtype {data.dtype} cannot be converted to timedelta64[ns]")
- data = np.array(data, copy=copy)
+ if not copy:
+ data = np.asarray(data)
+ else:
+ data = np.array(data, copy=copy)
assert data.dtype.kind == "m"
assert data.dtype != "m8" # i.e. not unit-less
@@ -1152,7 +1155,7 @@ def _objects_to_td64ns(
higher level.
"""
# coerce Index to np.ndarray, converting string-dtype if necessary
- values = np.array(data, dtype=np.object_, copy=False)
+ values = np.asarray(data, dtype=np.object_)
result = array_to_timedelta64(values, unit=unit, errors=errors)
return result.view("timedelta64[ns]")
diff --git a/pandas/core/construction.py b/pandas/core/construction.py
index 7b35d451c1120..af2aea11dcf6d 100644
--- a/pandas/core/construction.py
+++ b/pandas/core/construction.py
@@ -626,7 +626,10 @@ def sanitize_array(
elif hasattr(data, "__array__"):
# e.g. dask array GH#38645
- data = np.array(data, copy=copy)
+ if not copy:
+ data = np.asarray(data)
+ else:
+ data = np.array(data, copy=copy)
return sanitize_array(
data,
index=index,
@@ -744,8 +747,11 @@ def _sanitize_str_dtypes(
# GH#19853: If data is a scalar, result has already the result
if not lib.is_scalar(data):
if not np.all(isna(data)):
- data = np.array(data, dtype=dtype, copy=False)
- result = np.array(data, dtype=object, copy=copy)
+ data = np.asarray(data, dtype=dtype)
+ if not copy:
+ result = np.asarray(data, dtype=object)
+ else:
+ result = np.array(data, dtype=object, copy=copy)
return result
@@ -810,6 +816,8 @@ def _try_cast(
# this will raise if we have e.g. floats
subarr = maybe_cast_to_integer_array(arr, dtype)
+ elif not copy:
+ subarr = np.asarray(arr, dtype=dtype)
else:
subarr = np.array(arr, dtype=dtype, copy=copy)
diff --git a/pandas/core/dtypes/cast.py b/pandas/core/dtypes/cast.py
index b8b73e7dc6ddb..01b7d500179bf 100644
--- a/pandas/core/dtypes/cast.py
+++ b/pandas/core/dtypes/cast.py
@@ -1503,7 +1503,10 @@ def construct_2d_arraylike_from_scalar(
# Attempt to coerce to a numpy array
try:
- arr = np.array(value, dtype=dtype, copy=copy)
+ if not copy:
+ arr = np.asarray(value, dtype=dtype)
+ else:
+ arr = np.array(value, dtype=dtype, copy=copy)
except (ValueError, TypeError) as err:
raise TypeError(
f"DataFrame constructor called with incompatible data and dtype: {err}"
@@ -1652,7 +1655,7 @@ def maybe_cast_to_integer_array(arr: list | np.ndarray, dtype: np.dtype) -> np.n
"out-of-bound Python int",
DeprecationWarning,
)
- casted = np.array(arr, dtype=dtype, copy=False)
+ casted = np.asarray(arr, dtype=dtype)
else:
with warnings.catch_warnings():
warnings.filterwarnings("ignore", category=RuntimeWarning)
diff --git a/pandas/core/dtypes/missing.py b/pandas/core/dtypes/missing.py
index ddfb7ea7f3696..97efb5db9baa9 100644
--- a/pandas/core/dtypes/missing.py
+++ b/pandas/core/dtypes/missing.py
@@ -564,7 +564,7 @@ def infer_fill_value(val):
"""
if not is_list_like(val):
val = [val]
- val = np.array(val, copy=False)
+ val = np.asarray(val)
if val.dtype.kind in "mM":
return np.array("NaT", dtype=val.dtype)
elif val.dtype == object:
diff --git a/pandas/core/frame.py b/pandas/core/frame.py
index d1d35506ad3a9..f0750aff6ed57 100644
--- a/pandas/core/frame.py
+++ b/pandas/core/frame.py
@@ -1919,7 +1919,7 @@ def to_numpy(
dtype = np.dtype(dtype)
result = self._mgr.as_array(dtype=dtype, copy=copy, na_value=na_value)
if result.dtype is not dtype:
- result = np.array(result, dtype=dtype, copy=False)
+ result = np.asarray(result, dtype=dtype)
return result
diff --git a/pandas/core/generic.py b/pandas/core/generic.py
index 1bc6b7a3eea03..53f0833dc5309 100644
--- a/pandas/core/generic.py
+++ b/pandas/core/generic.py
@@ -1989,7 +1989,9 @@ def empty(self) -> bool:
# GH#23114 Ensure ndarray.__op__(DataFrame) returns NotImplemented
__array_priority__: int = 1000
- def __array__(self, dtype: npt.DTypeLike | None = None) -> np.ndarray:
+ def __array__(
+ self, dtype: npt.DTypeLike | None = None, copy: bool | None = None
+ ) -> np.ndarray:
values = self._values
arr = np.asarray(values, dtype=dtype)
if astype_is_view(values.dtype, arr.dtype) and self._mgr.is_single_block:
diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py
index 0701bed7cd9a4..c72c5fa019bd7 100644
--- a/pandas/core/indexes/base.py
+++ b/pandas/core/indexes/base.py
@@ -912,7 +912,7 @@ def __len__(self) -> int:
"""
return len(self._data)
- def __array__(self, dtype=None) -> np.ndarray:
+ def __array__(self, dtype=None, copy=None) -> np.ndarray:
"""
The array interface, return my values.
"""
diff --git a/pandas/core/indexes/multi.py b/pandas/core/indexes/multi.py
index 119c86770af3e..a1ca9727c1dbf 100644
--- a/pandas/core/indexes/multi.py
+++ b/pandas/core/indexes/multi.py
@@ -770,7 +770,7 @@ def _values(self) -> np.ndarray:
):
vals = vals.astype(object)
- array_vals = np.array(vals, copy=False)
+ array_vals = np.asarray(vals)
array_vals = algos.take_nd(array_vals, codes, fill_value=index._na_value)
values.append(array_vals)
@@ -1330,7 +1330,7 @@ def copy( # type: ignore[override]
new_index._id = self._id
return new_index
- def __array__(self, dtype=None) -> np.ndarray:
+ def __array__(self, dtype=None, copy=None) -> np.ndarray:
"""the array interface, return my values"""
return self.values
@@ -3357,7 +3357,7 @@ def convert_indexer(start, stop, step, indexer=indexer, codes=level_codes):
locs = (level_codes >= idx.start) & (level_codes < idx.stop)
return locs
- locs = np.array(level_codes == idx, dtype=bool, copy=False)
+ locs = np.asarray(level_codes == idx, dtype=bool)
if not locs.any():
# The label is present in self.levels[level] but unused:
diff --git a/pandas/core/internals/managers.py b/pandas/core/internals/managers.py
index fe0e62784bd6a..f1cbfb39b0c10 100644
--- a/pandas/core/internals/managers.py
+++ b/pandas/core/internals/managers.py
@@ -1824,6 +1824,8 @@ def as_array(
na_value=na_value,
copy=copy,
).reshape(blk.shape)
+ elif not copy:
+ arr = np.asarray(blk.values, dtype=dtype)
else:
arr = np.array(blk.values, dtype=dtype, copy=copy)
diff --git a/pandas/core/series.py b/pandas/core/series.py
index bae95418c7641..d7aed54da9014 100644
--- a/pandas/core/series.py
+++ b/pandas/core/series.py
@@ -789,7 +789,9 @@ def __len__(self) -> int:
# ----------------------------------------------------------------------
# NDArray Compat
- def __array__(self, dtype: npt.DTypeLike | None = None) -> np.ndarray:
+ def __array__(
+ self, dtype: npt.DTypeLike | None = None, copy: bool | None = None
+ ) -> np.ndarray:
"""
Return the values as a NumPy array.
@@ -802,6 +804,9 @@ def __array__(self, dtype: npt.DTypeLike | None = None) -> np.ndarray:
The dtype to use for the resulting NumPy array. By default,
the dtype is inferred from the data.
+ copy : bool or None, optional
+ Unused.
+
Returns
-------
numpy.ndarray
diff --git a/pandas/io/pytables.py b/pandas/io/pytables.py
index c835a7365d158..60ef953059d18 100644
--- a/pandas/io/pytables.py
+++ b/pandas/io/pytables.py
@@ -4043,7 +4043,7 @@ def _create_axes(
if isinstance(data_converted.dtype, CategoricalDtype):
ordered = data_converted.ordered
meta = "category"
- metadata = np.array(data_converted.categories, copy=False).ravel()
+ metadata = np.asarray(data_converted.categories).ravel()
data, dtype_name = _get_data_and_dtype_name(data_converted)
diff --git a/pandas/tests/arrays/integer/test_arithmetic.py b/pandas/tests/arrays/integer/test_arithmetic.py
index d979dd445a61a..8acd298f37a07 100644
--- a/pandas/tests/arrays/integer/test_arithmetic.py
+++ b/pandas/tests/arrays/integer/test_arithmetic.py
@@ -197,6 +197,7 @@ def test_error_invalid_values(data, all_arithmetic_operators, using_infer_string
"Addition/subtraction of integers and integer-arrays with Timestamp",
"has no kernel",
"not implemented",
+ "The 'out' kwarg is necessary. Use numpy.strings.multiply without it.",
]
)
with pytest.raises(errs, match=msg):
diff --git a/pandas/tests/arrays/test_datetimelike.py b/pandas/tests/arrays/test_datetimelike.py
index ed915a8878c9a..b6ae1a9df0e65 100644
--- a/pandas/tests/arrays/test_datetimelike.py
+++ b/pandas/tests/arrays/test_datetimelike.py
@@ -12,6 +12,7 @@
Timestamp,
)
from pandas._libs.tslibs import to_offset
+from pandas.compat.numpy import np_version_gt2
from pandas.core.dtypes.dtypes import PeriodDtype
@@ -640,13 +641,14 @@ def test_round(self, arr1d):
def test_array_interface(self, datetime_index):
arr = datetime_index._data
+ copy_false = None if np_version_gt2 else False
# default asarray gives the same underlying data (for tz naive)
result = np.asarray(arr)
expected = arr._ndarray
assert result is expected
tm.assert_numpy_array_equal(result, expected)
- result = np.array(arr, copy=False)
+ result = np.array(arr, copy=copy_false)
assert result is expected
tm.assert_numpy_array_equal(result, expected)
@@ -655,7 +657,7 @@ def test_array_interface(self, datetime_index):
expected = arr._ndarray
assert result is expected
tm.assert_numpy_array_equal(result, expected)
- result = np.array(arr, dtype="datetime64[ns]", copy=False)
+ result = np.array(arr, dtype="datetime64[ns]", copy=copy_false)
assert result is expected
tm.assert_numpy_array_equal(result, expected)
result = np.array(arr, dtype="datetime64[ns]")
@@ -698,6 +700,7 @@ def test_array_tz(self, arr1d):
# GH#23524
arr = arr1d
dti = self.index_cls(arr1d)
+ copy_false = None if np_version_gt2 else False
expected = dti.asi8.view("M8[ns]")
result = np.array(arr, dtype="M8[ns]")
@@ -706,17 +709,18 @@ def test_array_tz(self, arr1d):
result = np.array(arr, dtype="datetime64[ns]")
tm.assert_numpy_array_equal(result, expected)
- # check that we are not making copies when setting copy=False
- result = np.array(arr, dtype="M8[ns]", copy=False)
+ # check that we are not making copies when setting copy=copy_false
+ result = np.array(arr, dtype="M8[ns]", copy=copy_false)
assert result.base is expected.base
assert result.base is not None
- result = np.array(arr, dtype="datetime64[ns]", copy=False)
+ result = np.array(arr, dtype="datetime64[ns]", copy=copy_false)
assert result.base is expected.base
assert result.base is not None
def test_array_i8_dtype(self, arr1d):
arr = arr1d
dti = self.index_cls(arr1d)
+ copy_false = None if np_version_gt2 else False
expected = dti.asi8
result = np.array(arr, dtype="i8")
@@ -725,8 +729,8 @@ def test_array_i8_dtype(self, arr1d):
result = np.array(arr, dtype=np.int64)
tm.assert_numpy_array_equal(result, expected)
- # check that we are still making copies when setting copy=False
- result = np.array(arr, dtype="i8", copy=False)
+ # check that we are still making copies when setting copy=copy_false
+ result = np.array(arr, dtype="i8", copy=copy_false)
assert result.base is not expected.base
assert result.base is None
@@ -952,13 +956,14 @@ def test_int_properties(self, timedelta_index, propname):
def test_array_interface(self, timedelta_index):
arr = timedelta_index._data
+ copy_false = None if np_version_gt2 else False
# default asarray gives the same underlying data
result = np.asarray(arr)
expected = arr._ndarray
assert result is expected
tm.assert_numpy_array_equal(result, expected)
- result = np.array(arr, copy=False)
+ result = np.array(arr, copy=copy_false)
assert result is expected
tm.assert_numpy_array_equal(result, expected)
@@ -967,7 +972,7 @@ def test_array_interface(self, timedelta_index):
expected = arr._ndarray
assert result is expected
tm.assert_numpy_array_equal(result, expected)
- result = np.array(arr, dtype="timedelta64[ns]", copy=False)
+ result = np.array(arr, dtype="timedelta64[ns]", copy=copy_false)
assert result is expected
tm.assert_numpy_array_equal(result, expected)
result = np.array(arr, dtype="timedelta64[ns]")
diff --git a/pandas/tests/dtypes/test_inference.py b/pandas/tests/dtypes/test_inference.py
index 0434ad7e50568..d54b15fbe6633 100644
--- a/pandas/tests/dtypes/test_inference.py
+++ b/pandas/tests/dtypes/test_inference.py
@@ -111,8 +111,8 @@ def it_outer():
def __len__(self) -> int:
return len(self._values)
- def __array__(self, t=None):
- return np.asarray(self._values, dtype=t)
+ def __array__(self, dtype=None, copy=None):
+ return np.asarray(self._values, dtype=dtype)
@property
def ndim(self):
diff --git a/pandas/tests/extension/array_with_attr/array.py b/pandas/tests/extension/array_with_attr/array.py
index d0249d9af8098..2789d51ec2ce3 100644
--- a/pandas/tests/extension/array_with_attr/array.py
+++ b/pandas/tests/extension/array_with_attr/array.py
@@ -49,7 +49,10 @@ def __init__(self, values, attr=None) -> None:
@classmethod
def _from_sequence(cls, scalars, *, dtype=None, copy=False):
- data = np.array(scalars, dtype="float64", copy=copy)
+ if not copy:
+ data = np.asarray(scalars, dtype="float64")
+ else:
+ data = np.array(scalars, dtype="float64", copy=copy)
return cls(data)
def __getitem__(self, item):
diff --git a/pandas/tests/extension/json/array.py b/pandas/tests/extension/json/array.py
index 31f44f886add7..e43b50322bb92 100644
--- a/pandas/tests/extension/json/array.py
+++ b/pandas/tests/extension/json/array.py
@@ -146,7 +146,7 @@ def __eq__(self, other):
def __ne__(self, other):
return NotImplemented
- def __array__(self, dtype=None):
+ def __array__(self, dtype=None, copy=None):
if dtype is None:
dtype = object
if dtype == object:
@@ -210,8 +210,10 @@ def astype(self, dtype, copy=True):
value = self.astype(str) # numpy doesn't like nested dicts
arr_cls = dtype.construct_array_type()
return arr_cls._from_sequence(value, dtype=dtype, copy=False)
-
- return np.array([dict(x) for x in self], dtype=dtype, copy=copy)
+ elif not copy:
+ return np.asarray([dict(x) for x in self], dtype=dtype)
+ else:
+ return np.array([dict(x) for x in self], dtype=dtype, copy=copy)
def unique(self):
# Parent method doesn't work since np.array will try to infer
diff --git a/pandas/tests/extension/list/array.py b/pandas/tests/extension/list/array.py
index f07585c0aec10..b3bb35c9396f4 100644
--- a/pandas/tests/extension/list/array.py
+++ b/pandas/tests/extension/list/array.py
@@ -115,7 +115,10 @@ def astype(self, dtype, copy=True):
elif is_string_dtype(dtype) and not is_object_dtype(dtype):
# numpy has problems with astype(str) for nested elements
return np.array([str(x) for x in self.data], dtype=dtype)
- return np.array(self.data, dtype=dtype, copy=copy)
+ elif not copy:
+ return np.asarray(self.data, dtype=dtype)
+ else:
+ return np.array(self.data, dtype=dtype, copy=copy)
@classmethod
def _concat_same_type(cls, to_concat):
diff --git a/pandas/tests/extension/test_common.py b/pandas/tests/extension/test_common.py
index 3d8523f344d46..5eda0f00f54ca 100644
--- a/pandas/tests/extension/test_common.py
+++ b/pandas/tests/extension/test_common.py
@@ -17,7 +17,7 @@ class DummyArray(ExtensionArray):
def __init__(self, data) -> None:
self.data = data
- def __array__(self, dtype):
+ def __array__(self, dtype=None, copy=None):
return self.data
@property
@@ -30,8 +30,10 @@ def astype(self, dtype, copy=True):
if copy:
return type(self)(self.data)
return self
-
- return np.array(self, dtype=dtype, copy=copy)
+ elif not copy:
+ return np.asarray(self, dtype=dtype)
+ else:
+ return np.array(self, dtype=dtype, copy=copy)
class TestExtensionArrayDtype:
diff --git a/pandas/tests/frame/methods/test_select_dtypes.py b/pandas/tests/frame/methods/test_select_dtypes.py
index 47c479faed1ef..d1bee6a3de613 100644
--- a/pandas/tests/frame/methods/test_select_dtypes.py
+++ b/pandas/tests/frame/methods/test_select_dtypes.py
@@ -32,7 +32,7 @@ def __init__(self, data, dtype) -> None:
self.data = data
self._dtype = dtype
- def __array__(self, dtype):
+ def __array__(self, dtype=None, copy=None):
return self.data
@property
diff --git a/pandas/tests/frame/test_arithmetic.py b/pandas/tests/frame/test_arithmetic.py
index 3cf6d31390c2f..f463b3f94fa55 100644
--- a/pandas/tests/frame/test_arithmetic.py
+++ b/pandas/tests/frame/test_arithmetic.py
@@ -57,7 +57,7 @@ def __init__(self, value, dtype) -> None:
self.value = value
self.dtype = np.dtype(dtype)
- def __array__(self):
+ def __array__(self, dtype=None, copy=None):
return np.array(self.value, dtype=self.dtype)
def __str__(self) -> str:
diff --git a/pandas/tests/indexes/test_index_new.py b/pandas/tests/indexes/test_index_new.py
index 867d32e5c86a2..2e61340023948 100644
--- a/pandas/tests/indexes/test_index_new.py
+++ b/pandas/tests/indexes/test_index_new.py
@@ -410,7 +410,7 @@ class ArrayLike:
def __init__(self, array) -> None:
self.array = array
- def __array__(self, dtype=None) -> np.ndarray:
+ def __array__(self, dtype=None, copy=None) -> np.ndarray:
return self.array
expected = Index(array)
| Hi!
This PR addresses changes planned for NumPy in https://github.com/numpy/numpy/pull/25168 (new `copy` keyword for `np.asarray` and `np.array`).
`np.array(..., copy=False)` will now throw and exception if a copy is needed. To retain the same behavior `np.asarray(...)` can be used, so a copy is made only when needed.
I applied changes in a backward-compatible way, so that no NumPy version check & branching is needed. | https://api.github.com/repos/pandas-dev/pandas/pulls/57172 | 2024-01-31T13:54:14Z | 2024-03-05T21:46:23Z | 2024-03-05T21:46:23Z | 2024-03-06T08:14:42Z |
REGR: Series.astype raising TypeError when converting a pyarrow integer type with nulls to numpy float | diff --git a/doc/source/whatsnew/v2.2.1.rst b/doc/source/whatsnew/v2.2.1.rst
index 69e14a9028dd3..cea2204ca8355 100644
--- a/doc/source/whatsnew/v2.2.1.rst
+++ b/doc/source/whatsnew/v2.2.1.rst
@@ -29,7 +29,7 @@ Fixed regressions
- Fixed regression in :meth:`ExtensionArray.to_numpy` raising for non-numeric masked dtypes (:issue:`56991`)
- Fixed regression in :meth:`Index.join` raising ``TypeError`` when joining an empty index to a non-empty index containing mixed dtype values (:issue:`57048`)
- Fixed regression in :meth:`Series.pct_change` raising a ``ValueError`` for an empty :class:`Series` (:issue:`57056`)
-- Fixed regression in :meth:`Series.to_numpy` when dtype is given as float and the data contains NaNs (:issue:`57121`)
+- Fixed regression in :meth:`Series.to_numpy` when dtype is given as float and the data contains NaNs (:issue:`57121`, :issue:`57093`)
.. ---------------------------------------------------------------------------
.. _whatsnew_221.bug_fixes:
diff --git a/pandas/core/arrays/_utils.py b/pandas/core/arrays/_utils.py
index 88091a88a4e12..88653052ae4e9 100644
--- a/pandas/core/arrays/_utils.py
+++ b/pandas/core/arrays/_utils.py
@@ -46,7 +46,16 @@ def to_numpy_dtype_inference(
dtype_given = True
if na_value is lib.no_default:
- na_value = arr.dtype.na_value
+ if dtype is None:
+ na_value = arr.dtype.na_value
+ elif dtype.kind == "f":
+ na_value = np.nan
+ elif dtype.kind == "M":
+ na_value = np.datetime64("nat")
+ elif dtype.kind == "m":
+ na_value = np.timedelta64("nat")
+ else:
+ na_value = arr.dtype.na_value
if not dtype_given and hasna:
try:
diff --git a/pandas/tests/extension/test_arrow.py b/pandas/tests/extension/test_arrow.py
index 9f8985788cb95..a7ca106f2388d 100644
--- a/pandas/tests/extension/test_arrow.py
+++ b/pandas/tests/extension/test_arrow.py
@@ -3480,3 +3480,12 @@ def test_to_numpy_timestamp_to_int():
result = ser.to_numpy(dtype=np.int64)
expected = np.array([1577853000000000000])
tm.assert_numpy_array_equal(result, expected)
+
+
+@pytest.mark.parametrize("dtype", ["float64", "datetime64[ns]", "timedelta64[ns]"])
+def test_astype_int_with_null_to_numpy_dtype(dtype):
+ # GH 57093
+ ser = pd.Series([1, None], dtype="int64[pyarrow]")
+ result = ser.astype(dtype)
+ expected = pd.Series([1, None], dtype=dtype)
+ tm.assert_series_equal(result, expected)
| - [x] closes #57093
- [x] [Tests added and passed](https://pandas.pydata.org/pandas-docs/dev/development/contributing_codebase.html#writing-tests) if fixing a bug or adding a new feature
- [x] All [code checks passed](https://pandas.pydata.org/pandas-docs/dev/development/contributing_codebase.html#pre-commit).
- [x] Added [type annotations](https://pandas.pydata.org/pandas-docs/dev/development/contributing_codebase.html#type-hints) to new arguments/methods/functions.
- [x] Added an entry in the latest `doc/source/whatsnew/v2.2.1.rst` file if fixing a bug or adding a new feature.
| https://api.github.com/repos/pandas-dev/pandas/pulls/57170 | 2024-01-31T12:31:12Z | 2024-02-10T01:23:03Z | null | 2024-02-10T01:23:03Z |
REGR: DataFrame.sort_index not producing stable sort | diff --git a/doc/source/whatsnew/v2.2.1.rst b/doc/source/whatsnew/v2.2.1.rst
index 19b7e3493f964..713e3787825fb 100644
--- a/doc/source/whatsnew/v2.2.1.rst
+++ b/doc/source/whatsnew/v2.2.1.rst
@@ -18,6 +18,7 @@ Fixed regressions
- Fixed regression in :func:`merge_ordered` raising ``TypeError`` for ``fill_method="ffill"`` and ``how="left"`` (:issue:`57010`)
- Fixed regression in :func:`wide_to_long` raising an ``AttributeError`` for string columns (:issue:`57066`)
- Fixed regression in :meth:`DataFrame.loc` raising ``IndexError`` for non-unique, masked dtype indexes where result has more than 10,000 rows (:issue:`57027`)
+- Fixed regression in :meth:`DataFrame.sort_index` not producing a stable sort for a index with duplicates (:issue:`57151`)
- Fixed regression in :meth:`DataFrame.to_dict` with ``orient='list'`` and datetime or timedelta types returning integers (:issue:`54824`)
- Fixed regression in :meth:`DataFrameGroupBy.idxmin`, :meth:`DataFrameGroupBy.idxmax`, :meth:`SeriesGroupBy.idxmin`, :meth:`SeriesGroupBy.idxmax` ignoring the ``skipna`` argument (:issue:`57040`)
- Fixed regression in :meth:`DataFrameGroupBy.idxmin`, :meth:`DataFrameGroupBy.idxmax`, :meth:`SeriesGroupBy.idxmin`, :meth:`SeriesGroupBy.idxmax` where values containing the minimum or maximum value for the dtype could produce incorrect results (:issue:`57040`)
diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py
index 47e00a5700db4..87135ce9e0dd0 100644
--- a/pandas/core/indexes/base.py
+++ b/pandas/core/indexes/base.py
@@ -5952,17 +5952,14 @@ def sort_values(
(Index([1000, 100, 10, 1], dtype='int64'), array([3, 1, 0, 2]))
"""
if key is None and (
- self.is_monotonic_increasing or self.is_monotonic_decreasing
+ (ascending and self.is_monotonic_increasing)
+ or (not ascending and self.is_monotonic_decreasing)
):
- reverse = ascending != self.is_monotonic_increasing
- sorted_index = self[::-1] if reverse else self.copy()
if return_indexer:
indexer = np.arange(len(self), dtype=np.intp)
- if reverse:
- indexer = indexer[::-1]
- return sorted_index, indexer
+ return self.copy(), indexer
else:
- return sorted_index
+ return self.copy()
# GH 35584. Sort missing values according to na_position kwarg
# ignore na_position for MultiIndex
diff --git a/pandas/tests/frame/methods/test_sort_index.py b/pandas/tests/frame/methods/test_sort_index.py
index 821b2e0d7a6da..1a5df0778ec52 100644
--- a/pandas/tests/frame/methods/test_sort_index.py
+++ b/pandas/tests/frame/methods/test_sort_index.py
@@ -1001,3 +1001,27 @@ def test_axis_columns_ignore_index():
result = df.sort_index(axis="columns", ignore_index=True)
expected = DataFrame([[2, 1]])
tm.assert_frame_equal(result, expected)
+
+
+def test_sort_index_stable_sort():
+ # GH 57151
+ df = DataFrame(
+ data=[
+ (Timestamp("2024-01-30 13:00:00"), 13.0),
+ (Timestamp("2024-01-30 13:00:00"), 13.1),
+ (Timestamp("2024-01-30 12:00:00"), 12.0),
+ (Timestamp("2024-01-30 12:00:00"), 12.1),
+ ],
+ columns=["dt", "value"],
+ ).set_index(["dt"])
+ result = df.sort_index(level="dt", kind="stable")
+ expected = DataFrame(
+ data=[
+ (Timestamp("2024-01-30 12:00:00"), 12.0),
+ (Timestamp("2024-01-30 12:00:00"), 12.1),
+ (Timestamp("2024-01-30 13:00:00"), 13.0),
+ (Timestamp("2024-01-30 13:00:00"), 13.1),
+ ],
+ columns=["dt", "value"],
+ ).set_index(["dt"])
+ tm.assert_frame_equal(result, expected)
| - [x] closes #57151
- [x] [Tests added and passed](https://pandas.pydata.org/pandas-docs/dev/development/contributing_codebase.html#writing-tests) if fixing a bug or adding a new feature
- [x] All [code checks passed](https://pandas.pydata.org/pandas-docs/dev/development/contributing_codebase.html#pre-commit).
- [x] Added [type annotations](https://pandas.pydata.org/pandas-docs/dev/development/contributing_codebase.html#type-hints) to new arguments/methods/functions.
- [x] Added an entry in the latest `doc/source/whatsnew/v2.2.1.rst` file if fixing a bug or adding a new feature.
| https://api.github.com/repos/pandas-dev/pandas/pulls/57169 | 2024-01-31T11:37:11Z | 2024-01-31T18:20:11Z | 2024-01-31T18:20:11Z | 2024-01-31T18:20:17Z |
Fix docstring | diff --git a/ci/code_checks.sh b/ci/code_checks.sh
index f3023bea3743e..57bc89f526afc 100755
--- a/ci/code_checks.sh
+++ b/ci/code_checks.sh
@@ -93,7 +93,6 @@ if [[ -z "$CHECK" || "$CHECK" == "docstrings" ]]; then
pandas.TimedeltaIndex.ceil\
pandas.PeriodIndex\
pandas.PeriodIndex.strftime\
- pandas.Series.clip\
pandas.Series.rename_axis\
pandas.Series.dt.to_period\
pandas.Series.dt.tz_localize\
@@ -115,7 +114,6 @@ if [[ -z "$CHECK" || "$CHECK" == "docstrings" ]]; then
pandas.Series.plot.barh\
pandas.Series.plot.line\
pandas.Series.plot.pie\
- pandas.DataFrame.clip\
pandas.DataFrame.plot\
pandas.DataFrame.plot.bar\
pandas.DataFrame.plot.barh\
@@ -183,8 +181,7 @@ if [[ -z "$CHECK" || "$CHECK" == "docstrings" ]]; then
pandas.core.groupby.DataFrameGroupBy.plot\
pandas.core.groupby.SeriesGroupBy.plot\
pandas.core.window.rolling.Rolling.quantile\
- pandas.core.window.expanding.Expanding.quantile\
- pandas.api.extensions.ExtensionArray.argsort # There should be no backslash in the final line, please keep this comment in the last ignored function
+ pandas.core.window.expanding.Expanding.quantile # There should be no backslash in the final line, please keep this comment in the last ignored function
RET=$(($RET + $?)) ; echo $MSG "DONE"
fi
diff --git a/pandas/core/arrays/base.py b/pandas/core/arrays/base.py
index b5946594fc7fc..e41a96cfcef7e 100644
--- a/pandas/core/arrays/base.py
+++ b/pandas/core/arrays/base.py
@@ -818,7 +818,7 @@ def argsort(
na_position : {'first', 'last'}, default 'last'
If ``'first'``, put ``NaN`` values at the beginning.
If ``'last'``, put ``NaN`` values at the end.
- *args, **kwargs:
+ **kwargs
Passed through to :func:`numpy.argsort`.
Returns
diff --git a/pandas/core/generic.py b/pandas/core/generic.py
index 87a394761ee7c..bde0282d33fc1 100644
--- a/pandas/core/generic.py
+++ b/pandas/core/generic.py
@@ -8962,7 +8962,7 @@ def clip(
For `Series` this parameter is unused and defaults to `None`.
inplace : bool, default False
Whether to perform the operation in place on the data.
- *args, **kwargs
+ **kwargs
Additional keywords have no effect but might be accepted
for compatibility with numpy.
| - [ ] xref #57111 (Replace xxxx with the GitHub issue number)
- [ ] [Tests added and passed](https://pandas.pydata.org/pandas-docs/dev/development/contributing_codebase.html#writing-tests) if fixing a bug or adding a new feature
- [ ] All [code checks passed](https://pandas.pydata.org/pandas-docs/dev/development/contributing_codebase.html#pre-commit).
- [ ] Added [type annotations](https://pandas.pydata.org/pandas-docs/dev/development/contributing_codebase.html#type-hints) to new arguments/methods/functions.
- [ ] Added an entry in the latest `doc/source/whatsnew/vX.X.X.rst` file if fixing a bug or adding a new feature.
| https://api.github.com/repos/pandas-dev/pandas/pulls/57167 | 2024-01-31T08:24:55Z | 2024-01-31T17:51:34Z | 2024-01-31T17:51:34Z | 2024-01-31T17:51:43Z |
Fix docstring | diff --git a/ci/code_checks.sh b/ci/code_checks.sh
index f3023bea3743e..0f4f0d9129c16 100755
--- a/ci/code_checks.sh
+++ b/ci/code_checks.sh
@@ -93,7 +93,6 @@ if [[ -z "$CHECK" || "$CHECK" == "docstrings" ]]; then
pandas.TimedeltaIndex.ceil\
pandas.PeriodIndex\
pandas.PeriodIndex.strftime\
- pandas.Series.clip\
pandas.Series.rename_axis\
pandas.Series.dt.to_period\
pandas.Series.dt.tz_localize\
@@ -183,8 +182,8 @@ if [[ -z "$CHECK" || "$CHECK" == "docstrings" ]]; then
pandas.core.groupby.DataFrameGroupBy.plot\
pandas.core.groupby.SeriesGroupBy.plot\
pandas.core.window.rolling.Rolling.quantile\
- pandas.core.window.expanding.Expanding.quantile\
- pandas.api.extensions.ExtensionArray.argsort # There should be no backslash in the final line, please keep this comment in the last ignored function
+ pandas.core.window.expanding.Expanding.quantile # There should be no backslash in the final line, please keep this comment in the last ignored function
+
RET=$(($RET + $?)) ; echo $MSG "DONE"
fi
diff --git a/pandas/core/arrays/base.py b/pandas/core/arrays/base.py
index b5946594fc7fc..b220f6c12d634 100644
--- a/pandas/core/arrays/base.py
+++ b/pandas/core/arrays/base.py
@@ -799,10 +799,10 @@ def _values_for_argsort(self) -> np.ndarray:
def argsort(
self,
- *,
ascending: bool = True,
kind: SortKind = "quicksort",
na_position: str = "last",
+ *args,
**kwargs,
) -> np.ndarray:
"""
@@ -818,7 +818,7 @@ def argsort(
na_position : {'first', 'last'}, default 'last'
If ``'first'``, put ``NaN`` values at the beginning.
If ``'last'``, put ``NaN`` values at the end.
- *args, **kwargs:
+ *args, **kwargs
Passed through to :func:`numpy.argsort`.
Returns
diff --git a/pandas/core/generic.py b/pandas/core/generic.py
index 87a394761ee7c..e7068d7c51928 100644
--- a/pandas/core/generic.py
+++ b/pandas/core/generic.py
@@ -8935,9 +8935,9 @@ def clip(
self,
lower=None,
upper=None,
- *,
axis: Axis | None = None,
inplace: bool_t = False,
+ *args,
**kwargs,
) -> Self | None:
"""
| - [ ] xref #27977 (Replace xxxx with the GitHub issue number)
- [ ] [Tests added and passed](https://pandas.pydata.org/pandas-docs/dev/development/contributing_codebase.html#writing-tests) if fixing a bug or adding a new feature
- [ ] All [code checks passed](https://pandas.pydata.org/pandas-docs/dev/development/contributing_codebase.html#pre-commit).
- [ ] Added [type annotations](https://pandas.pydata.org/pandas-docs/dev/development/contributing_codebase.html#type-hints) to new arguments/methods/functions.
- [ ] Added an entry in the latest `doc/source/whatsnew/vX.X.X.rst` file if fixing a bug or adding a new feature.
| https://api.github.com/repos/pandas-dev/pandas/pulls/57166 | 2024-01-31T07:16:54Z | 2024-01-31T08:11:14Z | null | 2024-01-31T08:11:22Z |
WEB: Adding Effective pandas 2 book to home page | diff --git a/web/pandas/getting_started.md b/web/pandas/getting_started.md
index cb14e52edad2c..17ff14caf1b60 100644
--- a/web/pandas/getting_started.md
+++ b/web/pandas/getting_started.md
@@ -42,7 +42,7 @@ The book we recommend to learn pandas is [Python for Data Analysis](https://amzn
by [Wes McKinney](https://wesmckinney.com/), creator of pandas.
<a href="https://amzn.to/3DyLaJc">
- <img alt="Python for Data Analysis" src="{{ base_url }}static/img/pydata_book.gif"/>
+ <img alt="Python for Data Analysis" src="{{ base_url }}static/img/books/pydata_book.gif"/>
</a>
## Videos
diff --git a/web/pandas/index.html b/web/pandas/index.html
index 3d2e5363a2a2d..4ad1095e0464e 100644
--- a/web/pandas/index.html
+++ b/web/pandas/index.html
@@ -90,10 +90,15 @@ <h4>Follow us</h4>
</ul>
</div>
- <h4>Get the book</h4>
+ <h4>Recommended books</h4>
<p class="book">
<a href="https://amzn.to/3DyLaJc">
- <img class="img-fluid" alt="Python for Data Analysis" src="{{ base_url }}static/img/pydata_book.gif"/>
+ <img class="img-fluid" alt="Python for Data Analysis" src="{{ base_url }}static/img/books/pydata_book.gif"/>
+ </a>
+ </p>
+ <p class="book">
+ <a href="https://store.metasnake.com/effective-pandas-book/lhte7">
+ <img class="img-fluid" alt="Effective pandas 2" src="{{ base_url }}static/img/books/effective_pandas_2.gif"/>
</a>
</p>
{% if releases[1:5] %}
diff --git a/web/pandas/static/img/books/effective_pandas_2.gif b/web/pandas/static/img/books/effective_pandas_2.gif
new file mode 100644
index 0000000000000..ae3efbcbd18a8
Binary files /dev/null and b/web/pandas/static/img/books/effective_pandas_2.gif differ
diff --git a/web/pandas/static/img/pydata_book.gif b/web/pandas/static/img/books/pydata_book.gif
similarity index 100%
rename from web/pandas/static/img/pydata_book.gif
rename to web/pandas/static/img/books/pydata_book.gif
| @mattharrison I think you discussed this with @phofl, I need the affiliated link before it's published.
Also, I changed the color palette of the book cover to reduce the size of the file. I don't think it's easy to notice, but if you want to have a look, to make sure you're happy with it. Thanks!
| https://api.github.com/repos/pandas-dev/pandas/pulls/57164 | 2024-01-31T05:23:00Z | 2024-02-10T15:47:13Z | 2024-02-10T15:47:13Z | 2024-02-10T15:47:22Z |
CI: Add macOS M1 CI | diff --git a/.github/workflows/unit-tests.yml b/.github/workflows/unit-tests.yml
index a3cffb4b03b93..4c7aa1e1e49ee 100644
--- a/.github/workflows/unit-tests.yml
+++ b/.github/workflows/unit-tests.yml
@@ -214,7 +214,8 @@ jobs:
timeout-minutes: 90
strategy:
matrix:
- os: [macos-latest, windows-latest]
+ # Note: Don't use macOS latest since macos 14 appears to be arm64 only
+ os: [macos-13, macos-14, windows-latest]
env_file: [actions-39.yaml, actions-310.yaml, actions-311.yaml, actions-312.yaml]
fail-fast: false
runs-on: ${{ matrix.os }}
@@ -227,8 +228,7 @@ jobs:
PANDAS_CI: 1
PYTEST_TARGET: pandas
PATTERN: "not slow and not db and not network and not single_cpu"
- # GH 47443: PYTEST_WORKERS > 0 crashes Windows builds with memory related errors
- PYTEST_WORKERS: ${{ matrix.os == 'macos-latest' && 'auto' || '0' }}
+ PYTEST_WORKERS: 'auto'
steps:
- name: Checkout
@@ -354,7 +354,8 @@ jobs:
strategy:
fail-fast: false
matrix:
- os: [ubuntu-22.04, macOS-latest, windows-latest]
+ # Separate out macOS 13 and 14, since macOS 14 is arm64 only
+ os: [ubuntu-22.04, macOS-13, macOS-14, windows-latest]
timeout-minutes: 90
diff --git a/.github/workflows/wheels.yml b/.github/workflows/wheels.yml
index 6d3b9048a2122..f79b2c51b5f92 100644
--- a/.github/workflows/wheels.yml
+++ b/.github/workflows/wheels.yml
@@ -94,7 +94,9 @@ jobs:
buildplat:
- [ubuntu-22.04, manylinux_x86_64]
- [ubuntu-22.04, musllinux_x86_64]
- - [macos-12, macosx_*]
+ - [macos-12, macosx_x86_64]
+ # Note: M1 images on Github Actions start from macOS 14
+ - [macos-14, macosx_arm64]
- [windows-2022, win_amd64]
# TODO: support PyPy?
python: [["cp39", "3.9"], ["cp310", "3.10"], ["cp311", "3.11"], ["cp312", "3.12"]]
@@ -128,7 +130,7 @@ jobs:
# Python version used to build sdist doesn't matter
# wheel will be built from sdist with the correct version
- name: Unzip sdist (macOS)
- if: ${{ matrix.buildplat[1] == 'macosx_*' }}
+ if: ${{ startsWith(matrix.buildplat[1], 'macosx') }}
run: |
tar -xzf ./dist/${{ env.sdist_name }} -C ./dist
@@ -139,18 +141,18 @@ jobs:
- name: Build normal wheels
if: ${{ (env.IS_SCHEDULE_DISPATCH != 'true' || env.IS_PUSH == 'true') }}
- uses: pypa/cibuildwheel@v2.16.4
+ uses: pypa/cibuildwheel@v2.16.5
with:
- package-dir: ./dist/${{ matrix.buildplat[1] == 'macosx_*' && env.sdist_name || needs.build_sdist.outputs.sdist_file }}
+ package-dir: ./dist/${{ startsWith(matrix.buildplat[1], 'macosx') && env.sdist_name || needs.build_sdist.outputs.sdist_file }}
env:
CIBW_PRERELEASE_PYTHONS: True
CIBW_BUILD: ${{ matrix.python[0] }}-${{ matrix.buildplat[1] }}
- name: Build nightly wheels (with NumPy pre-release)
if: ${{ (env.IS_SCHEDULE_DISPATCH == 'true' && env.IS_PUSH != 'true') }}
- uses: pypa/cibuildwheel@v2.16.4
+ uses: pypa/cibuildwheel@v2.16.5
with:
- package-dir: ./dist/${{ matrix.buildplat[1] == 'macosx_*' && env.sdist_name || needs.build_sdist.outputs.sdist_file }}
+ package-dir: ./dist/${{ startsWith(matrix.buildplat[1], 'macosx') && env.sdist_name || needs.build_sdist.outputs.sdist_file }}
env:
# The nightly wheels should be build witht he NumPy 2.0 pre-releases
# which requires the additional URL.
@@ -183,7 +185,7 @@ jobs:
$TST_CMD = @"
python -m pip install hypothesis>=6.46.1 pytest>=7.3.2 pytest-xdist>=2.2.0;
python -m pip install `$(Get-Item pandas\wheelhouse\*.whl);
- python -c `'import pandas as pd; pd.test(extra_args=[\"`\"--no-strict-data-files`\"\", \"`\"-m not clipboard and not single_cpu and not slow and not network and not db`\"\"])`';
+ python -c `'import pandas as pd; pd.test(extra_args=[`\"--no-strict-data-files`\", `\"-m not clipboard and not single_cpu and not slow and not network and not db`\"])`';
"@
# add rc to the end of the image name if the Python version is unreleased
docker pull python:${{ matrix.python[1] == '3.12' && '3.12-rc' || format('{0}-windowsservercore', matrix.python[1]) }}
@@ -191,7 +193,7 @@ jobs:
- uses: actions/upload-artifact@v4
with:
- name: ${{ matrix.python[0] }}-${{ startsWith(matrix.buildplat[1], 'macosx') && 'macosx' || matrix.buildplat[1] }}
+ name: ${{ matrix.python[0] }}-${{ matrix.buildplat[1] }}
path: ./wheelhouse/*.whl
- name: Upload wheels & sdist
diff --git a/ci/deps/actions-310.yaml b/ci/deps/actions-310.yaml
index 45f114322015b..a3e44e6373145 100644
--- a/ci/deps/actions-310.yaml
+++ b/ci/deps/actions-310.yaml
@@ -14,7 +14,6 @@ dependencies:
- pytest>=7.3.2
- pytest-cov
- pytest-xdist>=2.2.0
- - pytest-localserver>=0.7.1
- pytest-qt>=4.2.0
- boto3
@@ -61,3 +60,4 @@ dependencies:
- adbc-driver-postgresql>=0.8.0
- adbc-driver-sqlite>=0.8.0
- tzdata>=2022.7
+ - pytest-localserver>=0.7.1
diff --git a/ci/deps/actions-311.yaml b/ci/deps/actions-311.yaml
index d14686696e669..95cd1a4d46ef4 100644
--- a/ci/deps/actions-311.yaml
+++ b/ci/deps/actions-311.yaml
@@ -14,7 +14,6 @@ dependencies:
- pytest>=7.3.2
- pytest-cov
- pytest-xdist>=2.2.0
- - pytest-localserver>=0.7.1
- pytest-qt>=4.2.0
- boto3
@@ -60,4 +59,4 @@ dependencies:
- pip:
- adbc-driver-postgresql>=0.8.0
- adbc-driver-sqlite>=0.8.0
- - tzdata>=2022.7
+ - pytest-localserver>=0.7.1
diff --git a/ci/deps/actions-312.yaml b/ci/deps/actions-312.yaml
index 86aaf24b4e15c..a442ed6feeb5d 100644
--- a/ci/deps/actions-312.yaml
+++ b/ci/deps/actions-312.yaml
@@ -14,7 +14,6 @@ dependencies:
- pytest>=7.3.2
- pytest-cov
- pytest-xdist>=2.2.0
- - pytest-localserver>=0.7.1
- pytest-qt>=4.2.0
- boto3
@@ -61,3 +60,4 @@ dependencies:
- adbc-driver-postgresql>=0.8.0
- adbc-driver-sqlite>=0.8.0
- tzdata>=2022.7
+ - pytest-localserver>=0.7.1
diff --git a/ci/deps/actions-39.yaml b/ci/deps/actions-39.yaml
index 31ee74174cd46..b162a78e7f115 100644
--- a/ci/deps/actions-39.yaml
+++ b/ci/deps/actions-39.yaml
@@ -14,7 +14,6 @@ dependencies:
- pytest>=7.3.2
- pytest-cov
- pytest-xdist>=2.2.0
- - pytest-localserver>=0.7.1
- pytest-qt>=4.2.0
- boto3
@@ -61,3 +60,4 @@ dependencies:
- adbc-driver-postgresql>=0.8.0
- adbc-driver-sqlite>=0.8.0
- tzdata>=2022.7
+ - pytest-localserver>=0.7.1
diff --git a/pyproject.toml b/pyproject.toml
index ffd3a7e1f1f33..c64071dbcb082 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -161,10 +161,6 @@ test-command = """
pd.test(extra_args=["-m not clipboard and single_cpu and not slow and not network and not db", "--no-strict-data-files"]);' \
"""
-[tool.cibuildwheel.macos]
-archs = "x86_64 arm64"
-test-skip = "*_arm64"
-
[tool.cibuildwheel.windows]
before-build = "pip install delvewheel"
repair-wheel-command = "delvewheel repair -w {dest_dir} {wheel}"
| - [ ] closes #47443 (Replace xxxx with the GitHub issue number)
- [ ] [Tests added and passed](https://pandas.pydata.org/pandas-docs/dev/development/contributing_codebase.html#writing-tests) if fixing a bug or adding a new feature
- [ ] All [code checks passed](https://pandas.pydata.org/pandas-docs/dev/development/contributing_codebase.html#pre-commit).
- [ ] Added [type annotations](https://pandas.pydata.org/pandas-docs/dev/development/contributing_codebase.html#type-hints) to new arguments/methods/functions.
- [ ] Added an entry in the latest `doc/source/whatsnew/vX.X.X.rst` file if fixing a bug or adding a new feature.
| https://api.github.com/repos/pandas-dev/pandas/pulls/57163 | 2024-01-31T02:23:18Z | 2024-02-02T00:53:05Z | 2024-02-02T00:53:05Z | 2024-02-02T00:53:24Z |
Timestamp 'freq' is removed in GH 14146, not moved to kw-only arg. Removal is … | diff --git a/doc/source/whatsnew/v2.0.0.rst b/doc/source/whatsnew/v2.0.0.rst
index be63da09846c8..16f53719ff3a7 100644
--- a/doc/source/whatsnew/v2.0.0.rst
+++ b/doc/source/whatsnew/v2.0.0.rst
@@ -732,7 +732,7 @@ or, if your formats are all ISO8601 (but possibly not identically-formatted) ::
Other API changes
^^^^^^^^^^^^^^^^^
-- The ``freq``, ``tz``, ``nanosecond``, and ``unit`` keywords in the :class:`Timestamp` constructor are now keyword-only (:issue:`45307`, :issue:`32526`)
+- The ``tz``, ``nanosecond``, and ``unit`` keywords in the :class:`Timestamp` constructor are now keyword-only (:issue:`45307`, :issue:`32526`)
- Passing ``nanoseconds`` greater than 999 or less than 0 in :class:`Timestamp` now raises a ``ValueError`` (:issue:`48538`, :issue:`48255`)
- :func:`read_csv`: specifying an incorrect number of columns with ``index_col`` of now raises ``ParserError`` instead of ``IndexError`` when using the c parser.
- Default value of ``dtype`` in :func:`get_dummies` is changed to ``bool`` from ``uint8`` (:issue:`45848`)
| …already captured under 'Removal of prior version deprecations/changes'
- [ ] closes #xxxx (Replace xxxx with the GitHub issue number)
- [ ] [Tests added and passed](https://pandas.pydata.org/pandas-docs/dev/development/contributing_codebase.html#writing-tests) if fixing a bug or adding a new feature
- [ ] All [code checks passed](https://pandas.pydata.org/pandas-docs/dev/development/contributing_codebase.html#pre-commit).
- [ ] Added [type annotations](https://pandas.pydata.org/pandas-docs/dev/development/contributing_codebase.html#type-hints) to new arguments/methods/functions.
- [ ] Added an entry in the latest `doc/source/whatsnew/vX.X.X.rst` file if fixing a bug or adding a new feature.
| https://api.github.com/repos/pandas-dev/pandas/pulls/57162 | 2024-01-31T01:32:35Z | 2024-01-31T18:23:14Z | 2024-01-31T18:23:14Z | 2024-01-31T18:23:27Z |
Backport PR #57157 on branch 2.2.x (BUG: Fix to_dict with datelike types and orient=list) | diff --git a/doc/source/whatsnew/v2.2.1.rst b/doc/source/whatsnew/v2.2.1.rst
index 1302648c3fc9a..19b7e3493f964 100644
--- a/doc/source/whatsnew/v2.2.1.rst
+++ b/doc/source/whatsnew/v2.2.1.rst
@@ -18,6 +18,7 @@ Fixed regressions
- Fixed regression in :func:`merge_ordered` raising ``TypeError`` for ``fill_method="ffill"`` and ``how="left"`` (:issue:`57010`)
- Fixed regression in :func:`wide_to_long` raising an ``AttributeError`` for string columns (:issue:`57066`)
- Fixed regression in :meth:`DataFrame.loc` raising ``IndexError`` for non-unique, masked dtype indexes where result has more than 10,000 rows (:issue:`57027`)
+- Fixed regression in :meth:`DataFrame.to_dict` with ``orient='list'`` and datetime or timedelta types returning integers (:issue:`54824`)
- Fixed regression in :meth:`DataFrameGroupBy.idxmin`, :meth:`DataFrameGroupBy.idxmax`, :meth:`SeriesGroupBy.idxmin`, :meth:`SeriesGroupBy.idxmax` ignoring the ``skipna`` argument (:issue:`57040`)
- Fixed regression in :meth:`DataFrameGroupBy.idxmin`, :meth:`DataFrameGroupBy.idxmax`, :meth:`SeriesGroupBy.idxmin`, :meth:`SeriesGroupBy.idxmax` where values containing the minimum or maximum value for the dtype could produce incorrect results (:issue:`57040`)
- Fixed regression in :meth:`Index.join` raising ``TypeError`` when joining an empty index to a non-empty index containing mixed dtype values (:issue:`57048`)
diff --git a/pandas/core/methods/to_dict.py b/pandas/core/methods/to_dict.py
index 7bd4851425c3b..accbd92a91ed6 100644
--- a/pandas/core/methods/to_dict.py
+++ b/pandas/core/methods/to_dict.py
@@ -171,13 +171,9 @@ def to_dict(
return into_c(
(
k,
- list(
- map(
- maybe_box_native, v.to_numpy(na_value=box_na_values[i]).tolist()
- )
- )
+ list(map(maybe_box_native, v.to_numpy(na_value=box_na_values[i])))
if i in object_dtype_indices_as_set
- else v.to_numpy().tolist(),
+ else list(map(maybe_box_native, v.to_numpy())),
)
for i, (k, v) in enumerate(df.items())
)
diff --git a/pandas/tests/frame/methods/test_to_dict.py b/pandas/tests/frame/methods/test_to_dict.py
index 61f0ad30b4519..570f85a4a31ee 100644
--- a/pandas/tests/frame/methods/test_to_dict.py
+++ b/pandas/tests/frame/methods/test_to_dict.py
@@ -12,8 +12,11 @@
NA,
DataFrame,
Index,
+ Interval,
MultiIndex,
+ Period,
Series,
+ Timedelta,
Timestamp,
)
import pandas._testing as tm
@@ -519,3 +522,14 @@ def test_to_dict_pos_args_deprecation(self):
)
with tm.assert_produces_warning(FutureWarning, match=msg):
df.to_dict("records", {})
+
+
+@pytest.mark.parametrize(
+ "val", [Timestamp(2020, 1, 1), Timedelta(1), Period("2020"), Interval(1, 2)]
+)
+def test_to_dict_list_pd_scalars(val):
+ # GH 54824
+ df = DataFrame({"a": [val]})
+ result = df.to_dict(orient="list")
+ expected = {"a": [val]}
+ assert result == expected
| Backport PR #57157: BUG: Fix to_dict with datelike types and orient=list | https://api.github.com/repos/pandas-dev/pandas/pulls/57160 | 2024-01-31T00:18:40Z | 2024-01-31T01:52:25Z | 2024-01-31T01:52:25Z | 2024-01-31T01:52:26Z |
CLN: `to_dict` | diff --git a/pandas/core/frame.py b/pandas/core/frame.py
index 97f4eaa7c208a..d3f1c2970429e 100644
--- a/pandas/core/frame.py
+++ b/pandas/core/frame.py
@@ -89,7 +89,6 @@
find_common_type,
infer_dtype_from_scalar,
invalidate_string_dtypes,
- maybe_box_native,
maybe_downcast_to_dtype,
)
from pandas.core.dtypes.common import (
@@ -1983,28 +1982,6 @@ def to_numpy(
return result
- def _create_data_for_split_and_tight_to_dict(
- self, are_all_object_dtype_cols: bool, object_dtype_indices: list[int]
- ) -> list:
- """
- Simple helper method to create data for to ``to_dict(orient="split")`` and
- ``to_dict(orient="tight")`` to create the main output data
- """
- if are_all_object_dtype_cols:
- data = [
- list(map(maybe_box_native, t))
- for t in self.itertuples(index=False, name=None)
- ]
- else:
- data = [list(t) for t in self.itertuples(index=False, name=None)]
- if object_dtype_indices:
- # If we have object_dtype_cols, apply maybe_box_naive after list
- # comprehension for perf
- for row in data:
- for i in object_dtype_indices:
- row[i] = maybe_box_native(row[i])
- return data
-
@overload
def to_dict(
self,
diff --git a/pandas/core/methods/to_dict.py b/pandas/core/methods/to_dict.py
index accbd92a91ed6..a88cf88ead66e 100644
--- a/pandas/core/methods/to_dict.py
+++ b/pandas/core/methods/to_dict.py
@@ -24,11 +24,34 @@
from pandas.core import common as com
if TYPE_CHECKING:
+ from collections.abc import Generator
+
from pandas._typing import MutableMappingT
from pandas import DataFrame
+def create_data_for_split(
+ df: DataFrame, are_all_object_dtype_cols: bool, object_dtype_indices: list[int]
+) -> Generator[list, None, None]:
+ """
+ Simple helper method to create data for to ``to_dict(orient="split")``
+ to create the main output data
+ """
+ if are_all_object_dtype_cols:
+ for tup in df.itertuples(index=False, name=None):
+ yield list(map(maybe_box_native, tup))
+ else:
+ for tup in df.itertuples(index=False, name=None):
+ data = list(tup)
+ if object_dtype_indices:
+ # If we have object_dtype_cols, apply maybe_box_naive after
+ # for perf
+ for i in object_dtype_indices:
+ data[i] = maybe_box_native(data[i])
+ yield data
+
+
@overload
def to_dict(
df: DataFrame,
@@ -152,35 +175,38 @@ def to_dict(
# GH46470 Return quickly if orient series to avoid creating dtype objects
return into_c((k, v) for k, v in df.items())
+ if orient == "dict":
+ return into_c((k, v.to_dict(into=into)) for k, v in df.items())
+
box_native_indices = [
i
for i, col_dtype in enumerate(df.dtypes.values)
if col_dtype == np.dtype(object) or isinstance(col_dtype, ExtensionDtype)
]
- box_na_values = [
- lib.no_default if not isinstance(col_dtype, BaseMaskedDtype) else libmissing.NA
- for i, col_dtype in enumerate(df.dtypes.values)
- ]
- are_all_object_dtype_cols = len(box_native_indices) == len(df.dtypes)
- if orient == "dict":
- return into_c((k, v.to_dict(into=into)) for k, v in df.items())
+ are_all_object_dtype_cols = len(box_native_indices) == len(df.dtypes)
- elif orient == "list":
+ if orient == "list":
object_dtype_indices_as_set: set[int] = set(box_native_indices)
+ box_na_values = (
+ lib.no_default
+ if not isinstance(col_dtype, BaseMaskedDtype)
+ else libmissing.NA
+ for col_dtype in df.dtypes.values
+ )
return into_c(
(
k,
- list(map(maybe_box_native, v.to_numpy(na_value=box_na_values[i])))
+ list(map(maybe_box_native, v.to_numpy(na_value=box_na_value)))
if i in object_dtype_indices_as_set
else list(map(maybe_box_native, v.to_numpy())),
)
- for i, (k, v) in enumerate(df.items())
+ for i, (box_na_value, (k, v)) in enumerate(zip(box_na_values, df.items()))
)
elif orient == "split":
- data = df._create_data_for_split_and_tight_to_dict(
- are_all_object_dtype_cols, box_native_indices
+ data = list(
+ create_data_for_split(df, are_all_object_dtype_cols, box_native_indices)
)
return into_c(
@@ -192,10 +218,6 @@ def to_dict(
)
elif orient == "tight":
- data = df._create_data_for_split_and_tight_to_dict(
- are_all_object_dtype_cols, box_native_indices
- )
-
return into_c(
((("index", df.index.tolist()),) if index else ())
+ (
@@ -215,11 +237,9 @@ def to_dict(
elif orient == "records":
columns = df.columns.tolist()
if are_all_object_dtype_cols:
- rows = (
- dict(zip(columns, row)) for row in df.itertuples(index=False, name=None)
- )
return [
- into_c((k, maybe_box_native(v)) for k, v in row.items()) for row in rows
+ into_c(zip(columns, map(maybe_box_native, row)))
+ for row in df.itertuples(index=False, name=None)
]
else:
data = [
@@ -235,7 +255,7 @@ def to_dict(
for row in data:
for col in object_dtype_cols:
row[col] = maybe_box_native(row[col])
- return data
+ return data # type: ignore[return-value]
elif orient == "index":
if not df.index.is_unique:
@@ -248,24 +268,21 @@ def to_dict(
)
elif box_native_indices:
object_dtype_indices_as_set = set(box_native_indices)
- is_object_dtype_by_index = [
- i in object_dtype_indices_as_set for i in range(len(df.columns))
- ]
return into_c(
(
t[0],
{
- columns[i]: maybe_box_native(v)
- if is_object_dtype_by_index[i]
+ column: maybe_box_native(v)
+ if i in object_dtype_indices_as_set
else v
- for i, v in enumerate(t[1:])
+ for i, (column, v) in enumerate(zip(columns, t[1:]))
},
)
for t in df.itertuples(name=None)
)
else:
return into_c(
- (t[0], dict(zip(df.columns, t[1:]))) for t in df.itertuples(name=None)
+ (t[0], dict(zip(columns, t[1:]))) for t in df.itertuples(name=None)
)
else:
| * Moved `DataFrame._create_data_for_split_and_tight_to_dict` to an independent function
* Eliminated some extra loops | https://api.github.com/repos/pandas-dev/pandas/pulls/57159 | 2024-01-30T23:14:55Z | 2024-01-31T02:45:31Z | 2024-01-31T02:45:31Z | 2024-01-31T02:45:34Z |
BUG: Fix to_dict with datelike types and orient=list | diff --git a/doc/source/whatsnew/v2.2.1.rst b/doc/source/whatsnew/v2.2.1.rst
index 1302648c3fc9a..19b7e3493f964 100644
--- a/doc/source/whatsnew/v2.2.1.rst
+++ b/doc/source/whatsnew/v2.2.1.rst
@@ -18,6 +18,7 @@ Fixed regressions
- Fixed regression in :func:`merge_ordered` raising ``TypeError`` for ``fill_method="ffill"`` and ``how="left"`` (:issue:`57010`)
- Fixed regression in :func:`wide_to_long` raising an ``AttributeError`` for string columns (:issue:`57066`)
- Fixed regression in :meth:`DataFrame.loc` raising ``IndexError`` for non-unique, masked dtype indexes where result has more than 10,000 rows (:issue:`57027`)
+- Fixed regression in :meth:`DataFrame.to_dict` with ``orient='list'`` and datetime or timedelta types returning integers (:issue:`54824`)
- Fixed regression in :meth:`DataFrameGroupBy.idxmin`, :meth:`DataFrameGroupBy.idxmax`, :meth:`SeriesGroupBy.idxmin`, :meth:`SeriesGroupBy.idxmax` ignoring the ``skipna`` argument (:issue:`57040`)
- Fixed regression in :meth:`DataFrameGroupBy.idxmin`, :meth:`DataFrameGroupBy.idxmax`, :meth:`SeriesGroupBy.idxmin`, :meth:`SeriesGroupBy.idxmax` where values containing the minimum or maximum value for the dtype could produce incorrect results (:issue:`57040`)
- Fixed regression in :meth:`Index.join` raising ``TypeError`` when joining an empty index to a non-empty index containing mixed dtype values (:issue:`57048`)
diff --git a/pandas/core/methods/to_dict.py b/pandas/core/methods/to_dict.py
index 7bd4851425c3b..accbd92a91ed6 100644
--- a/pandas/core/methods/to_dict.py
+++ b/pandas/core/methods/to_dict.py
@@ -171,13 +171,9 @@ def to_dict(
return into_c(
(
k,
- list(
- map(
- maybe_box_native, v.to_numpy(na_value=box_na_values[i]).tolist()
- )
- )
+ list(map(maybe_box_native, v.to_numpy(na_value=box_na_values[i])))
if i in object_dtype_indices_as_set
- else v.to_numpy().tolist(),
+ else list(map(maybe_box_native, v.to_numpy())),
)
for i, (k, v) in enumerate(df.items())
)
diff --git a/pandas/tests/frame/methods/test_to_dict.py b/pandas/tests/frame/methods/test_to_dict.py
index 61f0ad30b4519..570f85a4a31ee 100644
--- a/pandas/tests/frame/methods/test_to_dict.py
+++ b/pandas/tests/frame/methods/test_to_dict.py
@@ -12,8 +12,11 @@
NA,
DataFrame,
Index,
+ Interval,
MultiIndex,
+ Period,
Series,
+ Timedelta,
Timestamp,
)
import pandas._testing as tm
@@ -519,3 +522,14 @@ def test_to_dict_pos_args_deprecation(self):
)
with tm.assert_produces_warning(FutureWarning, match=msg):
df.to_dict("records", {})
+
+
+@pytest.mark.parametrize(
+ "val", [Timestamp(2020, 1, 1), Timedelta(1), Period("2020"), Interval(1, 2)]
+)
+def test_to_dict_list_pd_scalars(val):
+ # GH 54824
+ df = DataFrame({"a": [val]})
+ result = df.to_dict(orient="list")
+ expected = {"a": [val]}
+ assert result == expected
| - [ ] closes #57125 (Replace xxxx with the GitHub issue number)
- [ ] [Tests added and passed](https://pandas.pydata.org/pandas-docs/dev/development/contributing_codebase.html#writing-tests) if fixing a bug or adding a new feature
- [ ] All [code checks passed](https://pandas.pydata.org/pandas-docs/dev/development/contributing_codebase.html#pre-commit).
- [ ] Added [type annotations](https://pandas.pydata.org/pandas-docs/dev/development/contributing_codebase.html#type-hints) to new arguments/methods/functions.
- [ ] Added an entry in the latest `doc/source/whatsnew/vX.X.X.rst` file if fixing a bug or adding a new feature.
| https://api.github.com/repos/pandas-dev/pandas/pulls/57157 | 2024-01-30T21:49:48Z | 2024-01-31T00:18:32Z | 2024-01-31T00:18:32Z | 2024-01-31T00:18:36Z |
CI: Remove redundant check | diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml
index de59c59affb22..ccd01abc4affe 100644
--- a/.pre-commit-config.yaml
+++ b/.pre-commit-config.yaml
@@ -101,8 +101,6 @@ repos:
- repo: https://github.com/pre-commit/pygrep-hooks
rev: v1.10.0
hooks:
- - id: python-check-blanket-noqa
- - id: python-check-blanket-type-ignore
- id: rst-backticks
- id: rst-directive-colons
types: [text] # overwrite types: [rst]
| I removed 2 checks that's already covered in `ruff`'s [`PGH003`](https://docs.astral.sh/ruff/rules/blanket-type-ignore/) and [`PGH004`](https://docs.astral.sh/ruff/rules/blanket-noqa/)
The second commit is just an example that the check works. Will revert before merging if approved.
```bash
ruff....................................................................................................Failed
- hook id: ruff
- exit code: 1
pandas/compat/numpy/function.py:209:18: PGH003 Use specific rule codes when ignoring type issues
pandas/tests/computation/test_compat.py:30:18: PGH004 Use specific rule codes when using `noqa`
Found 2 errors.
```
- [ ] closes #xxxx (Replace xxxx with the GitHub issue number)
- [ ] [Tests added and passed](https://pandas.pydata.org/pandas-docs/dev/development/contributing_codebase.html#writing-tests) if fixing a bug or adding a new feature
- [ ] All [code checks passed](https://pandas.pydata.org/pandas-docs/dev/development/contributing_codebase.html#pre-commit).
- [ ] Added [type annotations](https://pandas.pydata.org/pandas-docs/dev/development/contributing_codebase.html#type-hints) to new arguments/methods/functions.
- [ ] Added an entry in the latest `doc/source/whatsnew/vX.X.X.rst` file if fixing a bug or adding a new feature.
| https://api.github.com/repos/pandas-dev/pandas/pulls/57156 | 2024-01-30T21:38:09Z | 2024-01-31T18:27:37Z | 2024-01-31T18:27:37Z | 2024-01-31T19:02:56Z |
CLN: Remove pickle support pre-pandas 1.0 | diff --git a/doc/source/whatsnew/v3.0.0.rst b/doc/source/whatsnew/v3.0.0.rst
index c5ac2a800223b..a6d9e079b26fe 100644
--- a/doc/source/whatsnew/v3.0.0.rst
+++ b/doc/source/whatsnew/v3.0.0.rst
@@ -85,7 +85,7 @@ See :ref:`install.dependencies` and :ref:`install.optional_dependencies` for mor
Other API changes
^^^^^^^^^^^^^^^^^
- 3rd party ``py.path`` objects are no longer explicitly supported in IO methods. Use :py:class:`pathlib.Path` objects instead (:issue:`57091`)
--
+- pickled objects from pandas version less than ``1.0.0`` or Python versions less than or equal to ``2.7`` are no longer supported (:issue:`57155`)
.. ---------------------------------------------------------------------------
.. _whatsnew_300.deprecations:
diff --git a/pandas/_libs/tslibs/nattype.pyx b/pandas/_libs/tslibs/nattype.pyx
index cd5e6e521b79f..7a08e4ad4b260 100644
--- a/pandas/_libs/tslibs/nattype.pyx
+++ b/pandas/_libs/tslibs/nattype.pyx
@@ -76,7 +76,7 @@ cdef _nat_rdivide_op(self, other):
return NotImplemented
-def __nat_unpickle(*args):
+def _nat_unpickle(*args):
# return constant defined in the module
return c_NaT
@@ -360,7 +360,7 @@ class NaTType(_NaT):
return self.__reduce__()
def __reduce__(self):
- return (__nat_unpickle, (None, ))
+ return (_nat_unpickle, (None, ))
def __rtruediv__(self, other):
return _nat_rdivide_op(self, other)
diff --git a/pandas/compat/pickle_compat.py b/pandas/compat/pickle_compat.py
index ff589ebba4cf6..012a53789d76a 100644
--- a/pandas/compat/pickle_compat.py
+++ b/pandas/compat/pickle_compat.py
@@ -1,12 +1,12 @@
"""
-Support pre-0.12 series pickle compatibility.
+Pickle compatibility to pandas version 1.0
"""
from __future__ import annotations
import contextlib
-import copy
+import copyreg
import io
-import pickle as pkl
+import pickle
from typing import (
TYPE_CHECKING,
Any,
@@ -17,7 +17,6 @@
from pandas._libs.arrays import NDArrayBacked
from pandas._libs.tslibs import BaseOffset
-from pandas import Index
from pandas.core.arrays import (
DatetimeArray,
PeriodArray,
@@ -29,111 +28,19 @@
from collections.abc import Generator
-def load_reduce(self) -> None:
- stack = self.stack
- args = stack.pop()
- func = stack[-1]
-
- try:
- stack[-1] = func(*args)
- return
- except TypeError as err:
- # If we have a deprecated function,
- # try to replace and try again.
-
- msg = "_reconstruct: First argument must be a sub-type of ndarray"
-
- if msg in str(err):
- try:
- cls = args[0]
- stack[-1] = object.__new__(cls)
- return
- except TypeError:
- pass
- elif args and isinstance(args[0], type) and issubclass(args[0], BaseOffset):
- # TypeError: object.__new__(Day) is not safe, use Day.__new__()
- cls = args[0]
- stack[-1] = cls.__new__(*args)
- return
- elif args and issubclass(args[0], PeriodArray):
- cls = args[0]
- stack[-1] = NDArrayBacked.__new__(*args)
- return
-
- raise
-
-
# If classes are moved, provide compat here.
_class_locations_map = {
- ("pandas.core.sparse.array", "SparseArray"): ("pandas.core.arrays", "SparseArray"),
- # 15477
- ("pandas.core.base", "FrozenNDArray"): ("numpy", "ndarray"),
# Re-routing unpickle block logic to go through _unpickle_block instead
# for pandas <= 1.3.5
("pandas.core.internals.blocks", "new_block"): (
"pandas._libs.internals",
"_unpickle_block",
),
- ("pandas.core.indexes.frozen", "FrozenNDArray"): ("numpy", "ndarray"),
- ("pandas.core.base", "FrozenList"): ("pandas.core.indexes.frozen", "FrozenList"),
- # 10890
- ("pandas.core.series", "TimeSeries"): ("pandas.core.series", "Series"),
- ("pandas.sparse.series", "SparseTimeSeries"): (
- "pandas.core.sparse.series",
- "SparseSeries",
- ),
- # 12588, extensions moving
- ("pandas._sparse", "BlockIndex"): ("pandas._libs.sparse", "BlockIndex"),
- ("pandas.tslib", "Timestamp"): ("pandas._libs.tslib", "Timestamp"),
- # 18543 moving period
- ("pandas._period", "Period"): ("pandas._libs.tslibs.period", "Period"),
- ("pandas._libs.period", "Period"): ("pandas._libs.tslibs.period", "Period"),
- # 18014 moved __nat_unpickle from _libs.tslib-->_libs.tslibs.nattype
- ("pandas.tslib", "__nat_unpickle"): (
- "pandas._libs.tslibs.nattype",
- "__nat_unpickle",
- ),
- ("pandas._libs.tslib", "__nat_unpickle"): (
+ ("pandas._libs.tslibs.nattype", "__nat_unpickle"): (
"pandas._libs.tslibs.nattype",
- "__nat_unpickle",
- ),
- # 15998 top-level dirs moving
- ("pandas.sparse.array", "SparseArray"): (
- "pandas.core.arrays.sparse",
- "SparseArray",
+ "_nat_unpickle",
),
- ("pandas.indexes.base", "_new_Index"): ("pandas.core.indexes.base", "_new_Index"),
- ("pandas.indexes.base", "Index"): ("pandas.core.indexes.base", "Index"),
- ("pandas.indexes.numeric", "Int64Index"): (
- "pandas.core.indexes.base",
- "Index", # updated in 50775
- ),
- ("pandas.indexes.range", "RangeIndex"): ("pandas.core.indexes.range", "RangeIndex"),
- ("pandas.indexes.multi", "MultiIndex"): ("pandas.core.indexes.multi", "MultiIndex"),
- ("pandas.tseries.index", "_new_DatetimeIndex"): (
- "pandas.core.indexes.datetimes",
- "_new_DatetimeIndex",
- ),
- ("pandas.tseries.index", "DatetimeIndex"): (
- "pandas.core.indexes.datetimes",
- "DatetimeIndex",
- ),
- ("pandas.tseries.period", "PeriodIndex"): (
- "pandas.core.indexes.period",
- "PeriodIndex",
- ),
- # 19269, arrays moving
- ("pandas.core.categorical", "Categorical"): ("pandas.core.arrays", "Categorical"),
- # 19939, add timedeltaindex, float64index compat from 15998 move
- ("pandas.tseries.tdi", "TimedeltaIndex"): (
- "pandas.core.indexes.timedeltas",
- "TimedeltaIndex",
- ),
- ("pandas.indexes.numeric", "Float64Index"): (
- "pandas.core.indexes.base",
- "Index", # updated in 50775
- ),
- # 50775, remove Int64Index, UInt64Index & Float64Index from codabase
+ # 50775, remove Int64Index, UInt64Index & Float64Index from codebase
("pandas.core.indexes.numeric", "Int64Index"): (
"pandas.core.indexes.base",
"Index",
@@ -153,20 +60,29 @@ def load_reduce(self) -> None:
}
-# our Unpickler sub-class to override methods and some dispatcher
-# functions for compat and uses a non-public class of the pickle module.
-
+def load_reduce(self):
+ stack = self.stack
+ args = stack.pop()
+ func = stack[-1]
-class Unpickler(pkl._Unpickler):
- def find_class(self, module, name):
- # override superclass
- key = (module, name)
- module, name = _class_locations_map.get(key, key)
- return super().find_class(module, name)
+ try:
+ stack[-1] = func(*args)
+ return
+ except TypeError:
+ # If we have a deprecated function,
+ # try to replace and try again.
+ if args and isinstance(args[0], type) and issubclass(args[0], BaseOffset):
+ # TypeError: object.__new__(Day) is not safe, use Day.__new__()
+ cls = args[0]
+ stack[-1] = cls.__new__(*args)
+ return
+ elif args and issubclass(args[0], PeriodArray):
+ cls = args[0]
+ stack[-1] = NDArrayBacked.__new__(*args)
+ return
-Unpickler.dispatch = copy.copy(Unpickler.dispatch)
-Unpickler.dispatch[pkl.REDUCE[0]] = load_reduce
+ raise
def load_newobj(self) -> None:
@@ -174,9 +90,7 @@ def load_newobj(self) -> None:
cls = self.stack[-1]
# compat
- if issubclass(cls, Index):
- obj = object.__new__(cls)
- elif issubclass(cls, DatetimeArray) and not args:
+ if issubclass(cls, DatetimeArray) and not args:
arr = np.array([], dtype="M8[ns]")
obj = cls.__new__(cls, arr, arr.dtype)
elif issubclass(cls, TimedeltaArray) and not args:
@@ -190,29 +104,18 @@ def load_newobj(self) -> None:
self.stack[-1] = obj
-Unpickler.dispatch[pkl.NEWOBJ[0]] = load_newobj
-
-
-def load_newobj_ex(self) -> None:
- kwargs = self.stack.pop()
- args = self.stack.pop()
- cls = self.stack.pop()
-
- # compat
- if issubclass(cls, Index):
- obj = object.__new__(cls)
- else:
- obj = cls.__new__(cls, *args, **kwargs)
- self.append(obj)
-
+class Unpickler(pickle.Unpickler):
+ dispatch_table = copyreg.dispatch_table.copy()
+ dispatch_table[pickle.REDUCE[0]] = load_reduce
+ dispatch_table[pickle.NEWOBJ[0]] = load_newobj
-try:
- Unpickler.dispatch[pkl.NEWOBJ_EX[0]] = load_newobj_ex
-except (AttributeError, KeyError):
- pass
+ def find_class(self, module, name):
+ key = (module, name)
+ module, name = _class_locations_map.get(key, key)
+ return super().find_class(module, name)
-def load(fh, encoding: str | None = None, is_verbose: bool = False) -> Any:
+def load(fh, encoding: str | None = None) -> Any:
"""
Load a pickle, with a provided encoding,
@@ -220,20 +123,13 @@ def load(fh, encoding: str | None = None, is_verbose: bool = False) -> Any:
----------
fh : a filelike object
encoding : an optional encoding
- is_verbose : show exception output
"""
- try:
- fh.seek(0)
- if encoding is not None:
- up = Unpickler(fh, encoding=encoding)
- else:
- up = Unpickler(fh)
- # "Unpickler" has no attribute "is_verbose" [attr-defined]
- up.is_verbose = is_verbose # type: ignore[attr-defined]
-
- return up.load()
- except (ValueError, TypeError):
- raise
+ fh.seek(0)
+ if encoding is not None:
+ up = Unpickler(fh, encoding=encoding)
+ else:
+ up = Unpickler(fh)
+ return up.load()
def loads(
@@ -257,9 +153,9 @@ def patch_pickle() -> Generator[None, None, None]:
"""
Temporarily patch pickle to use our unpickler.
"""
- orig_loads = pkl.loads
+ orig_loads = pickle.loads
try:
- setattr(pkl, "loads", loads)
+ setattr(pickle, "loads", loads)
yield
finally:
- setattr(pkl, "loads", orig_loads)
+ setattr(pickle, "loads", orig_loads)
diff --git a/pandas/core/arrays/sparse/array.py b/pandas/core/arrays/sparse/array.py
index fafeedc01b02b..67be5c7fa0853 100644
--- a/pandas/core/arrays/sparse/array.py
+++ b/pandas/core/arrays/sparse/array.py
@@ -1375,23 +1375,6 @@ def _where(self, mask, value):
result = type(self)._from_sequence(naive_implementation, dtype=dtype)
return result
- # ------------------------------------------------------------------------
- # IO
- # ------------------------------------------------------------------------
- def __setstate__(self, state) -> None:
- """Necessary for making this object picklable"""
- if isinstance(state, tuple):
- # Compat for pandas < 0.24.0
- nd_state, (fill_value, sp_index) = state
- sparse_values = np.array([])
- sparse_values.__setstate__(nd_state)
-
- self._sparse_values = sparse_values
- self._sparse_index = sp_index
- self._dtype = SparseDtype(sparse_values.dtype, fill_value)
- else:
- self.__dict__.update(state)
-
def nonzero(self) -> tuple[npt.NDArray[np.int32]]:
if self.fill_value == 0:
return (self.sp_index.indices,)
diff --git a/pandas/core/internals/managers.py b/pandas/core/internals/managers.py
index c6a8b61e0c51e..60285f987cce1 100644
--- a/pandas/core/internals/managers.py
+++ b/pandas/core/internals/managers.py
@@ -1930,11 +1930,6 @@ def unpickle_block(values, mgr_locs, ndim: int) -> Block:
else:
raise NotImplementedError("pre-0.14.1 pickles are no longer supported")
- self._post_setstate()
-
- def _post_setstate(self) -> None:
- pass
-
@cache_readonly
def _block(self) -> Block:
return self.blocks[0]
diff --git a/pandas/io/pickle.py b/pandas/io/pickle.py
index 89867ab4f19d0..54facd89dc5d9 100644
--- a/pandas/io/pickle.py
+++ b/pandas/io/pickle.py
@@ -156,7 +156,7 @@ def read_pickle(
Notes
-----
- read_pickle is only guaranteed to be backwards compatible to pandas 0.20.3
+ read_pickle is only guaranteed to be backwards compatible to pandas 1.0.0
provided the object was serialized with to_pickle.
Examples
diff --git a/pandas/tests/io/data/legacy_hdf/periodindex_0.20.1_x86_64_darwin_2.7.13.h5 b/pandas/tests/io/data/legacy_hdf/periodindex_0.20.1_x86_64_darwin_2.7.13.h5
deleted file mode 100644
index 6fb92d3c564bd..0000000000000
Binary files a/pandas/tests/io/data/legacy_hdf/periodindex_0.20.1_x86_64_darwin_2.7.13.h5 and /dev/null differ
diff --git a/pandas/tests/io/data/pickle/categorical.0.25.0.pickle b/pandas/tests/io/data/pickle/categorical.0.25.0.pickle
deleted file mode 100644
index b756060c83d94..0000000000000
Binary files a/pandas/tests/io/data/pickle/categorical.0.25.0.pickle and /dev/null differ
diff --git a/pandas/tests/io/data/pickle/test_mi_py27.pkl b/pandas/tests/io/data/pickle/test_mi_py27.pkl
deleted file mode 100644
index 89021dd828108..0000000000000
Binary files a/pandas/tests/io/data/pickle/test_mi_py27.pkl and /dev/null differ
diff --git a/pandas/tests/io/data/pickle/test_py27.pkl b/pandas/tests/io/data/pickle/test_py27.pkl
deleted file mode 100644
index 5308b864bc0c7..0000000000000
Binary files a/pandas/tests/io/data/pickle/test_py27.pkl and /dev/null differ
diff --git a/pandas/tests/io/pytables/test_read.py b/pandas/tests/io/pytables/test_read.py
index c8563ee4af4a8..0fd0a8774bbf4 100644
--- a/pandas/tests/io/pytables/test_read.py
+++ b/pandas/tests/io/pytables/test_read.py
@@ -348,34 +348,6 @@ def test_read_hdf_series_mode_r(tmp_path, format, setup_path):
tm.assert_series_equal(result, series)
-@pytest.mark.filterwarnings(r"ignore:Period with BDay freq is deprecated:FutureWarning")
-@pytest.mark.filterwarnings(r"ignore:PeriodDtype\[B\] is deprecated:FutureWarning")
-def test_read_py2_hdf_file_in_py3(datapath):
- # GH 16781
-
- # tests reading a PeriodIndex DataFrame written in Python2 in Python3
-
- # the file was generated in Python 2.7 like so:
- #
- # df = DataFrame([1.,2,3], index=pd.PeriodIndex(
- # ['2015-01-01', '2015-01-02', '2015-01-05'], freq='B'))
- # df.to_hdf('periodindex_0.20.1_x86_64_darwin_2.7.13.h5', 'p')
-
- expected = DataFrame(
- [1.0, 2, 3],
- index=pd.PeriodIndex(["2015-01-01", "2015-01-02", "2015-01-05"], freq="B"),
- )
-
- with ensure_clean_store(
- datapath(
- "io", "data", "legacy_hdf", "periodindex_0.20.1_x86_64_darwin_2.7.13.h5"
- ),
- mode="r",
- ) as store:
- result = store["p"]
- tm.assert_frame_equal(result, expected)
-
-
def test_read_infer_string(tmp_path, setup_path):
# GH#54431
pytest.importorskip("pyarrow")
diff --git a/pandas/tests/io/test_common.py b/pandas/tests/io/test_common.py
index b43f430b8895b..41800dc4d7256 100644
--- a/pandas/tests/io/test_common.py
+++ b/pandas/tests/io/test_common.py
@@ -297,7 +297,13 @@ def test_read_expands_user_home_dir(
(
pd.read_pickle,
"os",
- ("io", "data", "pickle", "categorical.0.25.0.pickle"),
+ (
+ "io",
+ "data",
+ "legacy_pickle",
+ "1.4.2",
+ "1.4.2_x86_64_linux_3.9.7.pickle",
+ ),
),
],
)
@@ -310,10 +316,9 @@ def test_read_fspath_all(self, reader, module, path, datapath):
expected = reader(path)
if path.endswith(".pickle"):
- # categorical
- tm.assert_categorical_equal(result, expected)
- else:
- tm.assert_frame_equal(result, expected)
+ result = result["frame"]["float"]
+ expected = expected["frame"]["float"]
+ tm.assert_frame_equal(result, expected)
@pytest.mark.parametrize(
"writer_name, writer_kwargs, module",
diff --git a/pandas/tests/io/test_pickle.py b/pandas/tests/io/test_pickle.py
index 57c7829924531..5b8fddebd4ffa 100644
--- a/pandas/tests/io/test_pickle.py
+++ b/pandas/tests/io/test_pickle.py
@@ -399,31 +399,6 @@ def test_read(self, protocol, get_random_path):
tm.assert_frame_equal(df, df2)
-@pytest.mark.parametrize(
- ["pickle_file", "excols"],
- [
- ("test_py27.pkl", Index(["a", "b", "c"])),
- (
- "test_mi_py27.pkl",
- pd.MultiIndex.from_arrays([["a", "b", "c"], ["A", "B", "C"]]),
- ),
- ],
-)
-def test_unicode_decode_error(datapath, pickle_file, excols):
- # pickle file written with py27, should be readable without raising
- # UnicodeDecodeError, see GH#28645 and GH#31988
- path = datapath("io", "data", "pickle", pickle_file)
- df = pd.read_pickle(path)
-
- # just test the columns are correct since the values are random
- tm.assert_index_equal(df.columns, excols)
-
-
-# ---------------------
-# tests for buffer I/O
-# ---------------------
-
-
def test_pickle_buffer_roundtrip():
with tm.ensure_clean() as path:
df = DataFrame(
| For a major pandas 3.0 release, I think it's reasonable that pickle support for pre-pandas 1.0 can be removed by now. | https://api.github.com/repos/pandas-dev/pandas/pulls/57155 | 2024-01-30T20:08:58Z | 2024-02-12T22:13:58Z | null | 2024-02-21T21:10:03Z |
Backport PR #57144 on branch 2.2.x (CI: Fix _get_dst_hours for numpy 2.0 change) | diff --git a/pandas/_libs/tslibs/tzconversion.pyx b/pandas/_libs/tslibs/tzconversion.pyx
index 2c4f0cd14db13..e3facd3d9599b 100644
--- a/pandas/_libs/tslibs/tzconversion.pyx
+++ b/pandas/_libs/tslibs/tzconversion.pyx
@@ -607,7 +607,8 @@ cdef ndarray[int64_t] _get_dst_hours(
ndarray[uint8_t, cast=True] mismatch
ndarray[int64_t] delta, dst_hours
ndarray[intp_t] switch_idxs, trans_idx, grp, a_idx, b_idx, one_diff
- list trans_grp
+ # TODO: Can uncomment when numpy >=2 is the minimum
+ # tuple trans_grp
intp_t switch_idx
int64_t left, right
| Backport PR #57144: CI: Fix _get_dst_hours for numpy 2.0 change | https://api.github.com/repos/pandas-dev/pandas/pulls/57153 | 2024-01-30T16:34:14Z | 2024-01-30T17:46:41Z | 2024-01-30T17:46:41Z | 2024-01-30T17:46:41Z |
DOC: Updated community slack link | diff --git a/doc/source/development/community.rst b/doc/source/development/community.rst
index 9a4de3c2580ab..ccf7be8e47748 100644
--- a/doc/source/development/community.rst
+++ b/doc/source/development/community.rst
@@ -112,7 +112,7 @@ people who are hesitant to bring up their questions or ideas on a large public
mailing list or GitHub.
If this sounds like the right place for you, you are welcome to join using
-`this link <https://join.slack.com/t/pandas-dev-community/shared_invite/zt-1e2qgy1r6-PLCN8UOLEUAYoLdAsaJilw>`_!
+`this link <https://join.slack.com/t/pandas-dev-community/shared_invite/zt-2blg6u9k3-K6_XvMRDZWeH7Id274UeIg>`_!
Please remember to follow our `Code of Conduct <https://pandas.pydata.org/community/coc.html>`_,
and be aware that our admins are monitoring for irrelevant messages and will remove folks who use
our
| - [X] closes #57099 (Replace xxxx with the GitHub issue number)
| https://api.github.com/repos/pandas-dev/pandas/pulls/57146 | 2024-01-30T06:50:07Z | 2024-01-30T07:38:32Z | 2024-01-30T07:38:32Z | 2024-01-30T07:43:13Z |
DOC: fix PR02 errors in docstrings - pandas.core.groupby.SeriesGroupBy.idxmax, pandas.core.groupby.SeriesGroupBy.idxmin | diff --git a/ci/code_checks.sh b/ci/code_checks.sh
index c3ee7a4e262fc..50baa1cb0b19f 100755
--- a/ci/code_checks.sh
+++ b/ci/code_checks.sh
@@ -173,8 +173,6 @@ if [[ -z "$CHECK" || "$CHECK" == "docstrings" ]]; then
pandas.core.groupby.DataFrameGroupBy.transform\
pandas.core.groupby.DataFrameGroupBy.nth\
pandas.core.groupby.DataFrameGroupBy.rolling\
- pandas.core.groupby.SeriesGroupBy.idxmax\
- pandas.core.groupby.SeriesGroupBy.idxmin\
pandas.core.groupby.SeriesGroupBy.nth\
pandas.core.groupby.SeriesGroupBy.rolling\
pandas.core.groupby.DataFrameGroupBy.hist\
diff --git a/pandas/core/groupby/generic.py b/pandas/core/groupby/generic.py
index 1a23d237dca46..d37acd8312483 100644
--- a/pandas/core/groupby/generic.py
+++ b/pandas/core/groupby/generic.py
@@ -1042,12 +1042,104 @@ def nsmallest(
result = self._python_apply_general(f, data, not_indexed_same=True)
return result
- @doc(Series.idxmin.__doc__)
def idxmin(self, skipna: bool = True) -> Series:
+ """
+ Return the row label of the minimum value.
+
+ If multiple values equal the minimum, the first row label with that
+ value is returned.
+
+ Parameters
+ ----------
+ skipna : bool, default True
+ Exclude NA/null values. If the entire Series is NA, the result
+ will be NA.
+
+ Returns
+ -------
+ Index
+ Label of the minimum value.
+
+ Raises
+ ------
+ ValueError
+ If the Series is empty.
+
+ See Also
+ --------
+ numpy.argmin : Return indices of the minimum values
+ along the given axis.
+ DataFrame.idxmin : Return index of first occurrence of minimum
+ over requested axis.
+ Series.idxmax : Return index *label* of the first occurrence
+ of maximum of values.
+
+ Examples
+ --------
+ >>> ser = pd.Series([1, 2, 3, 4], index=pd.DatetimeIndex(
+ ... ['2023-01-01', '2023-01-15', '2023-02-01', '2023-02-15']))
+ >>> ser
+ 2023-01-01 1
+ 2023-01-15 2
+ 2023-02-01 3
+ 2023-02-15 4
+ dtype: int64
+
+ >>> ser.groupby(['a', 'a', 'b', 'b']).idxmin()
+ a 2023-01-01
+ b 2023-02-01
+ dtype: datetime64[ns]
+ """
return self._idxmax_idxmin("idxmin", skipna=skipna)
- @doc(Series.idxmax.__doc__)
def idxmax(self, skipna: bool = True) -> Series:
+ """
+ Return the row label of the maximum value.
+
+ If multiple values equal the maximum, the first row label with that
+ value is returned.
+
+ Parameters
+ ----------
+ skipna : bool, default True
+ Exclude NA/null values. If the entire Series is NA, the result
+ will be NA.
+
+ Returns
+ -------
+ Index
+ Label of the maximum value.
+
+ Raises
+ ------
+ ValueError
+ If the Series is empty.
+
+ See Also
+ --------
+ numpy.argmax : Return indices of the maximum values
+ along the given axis.
+ DataFrame.idxmax : Return index of first occurrence of maximum
+ over requested axis.
+ Series.idxmin : Return index *label* of the first occurrence
+ of minimum of values.
+
+ Examples
+ --------
+ >>> ser = pd.Series([1, 2, 3, 4], index=pd.DatetimeIndex(
+ ... ['2023-01-01', '2023-01-15', '2023-02-01', '2023-02-15']))
+ >>> ser
+ 2023-01-01 1
+ 2023-01-15 2
+ 2023-02-01 3
+ 2023-02-15 4
+ dtype: int64
+
+ >>> ser.groupby(['a', 'a', 'b', 'b']).idxmax()
+ a 2023-01-15
+ b 2023-02-15
+ dtype: datetime64[ns]
+ """
return self._idxmax_idxmin("idxmax", skipna=skipna)
@doc(Series.corr.__doc__)
| All PR02 Errors resolved in the following cases:
1. scripts/validate_docstrings.py --format=actions --errors=PR02 pandas.core.groupby.SeriesGroupBy.idxmax
2. scripts/validate_docstrings.py --format=actions --errors=PR02 pandas.core.groupby.SeriesGroupBy.idxmin
OUTPUT:
1. scripts/validate_docstrings.py --format=actions --errors=PR02 pandas.core.groupby.SeriesGroupBy.idxmax
```
################################################################################
################################## Validation ##################################
################################################################################
Docstring for "pandas.core.groupby.SeriesGroupBy.idxmax" correct. :)
```
2. scripts/validate_docstrings.py --format=actions --errors=PR02 pandas.core.groupby.SeriesGroupBy.idxmin
```
################################################################################
################################## Validation ##################################
################################################################################
Docstring for "pandas.core.groupby.SeriesGroupBy.idxmin" correct. :)
```
- [x] xref https://github.com/pandas-dev/pandas/issues/57111
- [x] [Tests added and passed](https://pandas.pydata.org/pandas-docs/dev/development/contributing_codebase.html#writing-tests) if fixing a bug or adding a new feature
- [x] All [code checks passed](https://pandas.pydata.org/pandas-docs/dev/development/contributing_codebase.html#pre-commit).
- [ ] Added [type annotations](https://pandas.pydata.org/pandas-docs/dev/development/contributing_codebase.html#type-hints) to new arguments/methods/functions.
- [ ] Added an entry in the latest `doc/source/whatsnew/vX.X.X.rst` file if fixing a bug or adding a new feature.
| https://api.github.com/repos/pandas-dev/pandas/pulls/57145 | 2024-01-30T05:45:50Z | 2024-02-01T21:34:49Z | 2024-02-01T21:34:49Z | 2024-02-01T21:52:54Z |
CI: Fix _get_dst_hours for numpy 2.0 change | diff --git a/pandas/_libs/tslibs/tzconversion.pyx b/pandas/_libs/tslibs/tzconversion.pyx
index 2c4f0cd14db13..e3facd3d9599b 100644
--- a/pandas/_libs/tslibs/tzconversion.pyx
+++ b/pandas/_libs/tslibs/tzconversion.pyx
@@ -607,7 +607,8 @@ cdef ndarray[int64_t] _get_dst_hours(
ndarray[uint8_t, cast=True] mismatch
ndarray[int64_t] delta, dst_hours
ndarray[intp_t] switch_idxs, trans_idx, grp, a_idx, b_idx, one_diff
- list trans_grp
+ # TODO: Can uncomment when numpy >=2 is the minimum
+ # tuple trans_grp
intp_t switch_idx
int64_t left, right
| xref https://github.com/numpy/numpy/pull/25570
I don't think we can make this annotation backwards compatible, so removing the annotation all together. | https://api.github.com/repos/pandas-dev/pandas/pulls/57144 | 2024-01-30T02:38:09Z | 2024-01-30T16:34:04Z | 2024-01-30T16:34:04Z | 2024-01-30T18:33:14Z |
Backport PR #57139 on branch 2.2.x (BUG: Index(Series) makes array read only for object dtype) | diff --git a/doc/source/whatsnew/v2.2.1.rst b/doc/source/whatsnew/v2.2.1.rst
index ff5fadec735e6..adf79ef5e7714 100644
--- a/doc/source/whatsnew/v2.2.1.rst
+++ b/doc/source/whatsnew/v2.2.1.rst
@@ -27,7 +27,7 @@ Fixed regressions
Bug fixes
~~~~~~~~~
--
+- Fixed bug in :meth:`DataFrame.__getitem__` for empty :class:`DataFrame` with Copy-on-Write enabled (:issue:`57130`)
.. ---------------------------------------------------------------------------
.. _whatsnew_221.other:
diff --git a/pandas/_libs/ops.pyx b/pandas/_libs/ops.pyx
index 9154e836b3477..567bfc02a2950 100644
--- a/pandas/_libs/ops.pyx
+++ b/pandas/_libs/ops.pyx
@@ -29,7 +29,7 @@ from pandas._libs.util cimport is_nan
@cython.wraparound(False)
@cython.boundscheck(False)
-def scalar_compare(object[:] values, object val, object op) -> ndarray:
+def scalar_compare(ndarray[object] values, object val, object op) -> ndarray:
"""
Compare each element of `values` array with the scalar `val`, with
the comparison operation described by `op`.
diff --git a/pandas/core/common.py b/pandas/core/common.py
index 7d864e02be54e..9f024498d66ed 100644
--- a/pandas/core/common.py
+++ b/pandas/core/common.py
@@ -233,6 +233,8 @@ def asarray_tuplesafe(values: Iterable, dtype: NpDtype | None = None) -> ArrayLi
values = list(values)
elif isinstance(values, ABCIndex):
return values._values
+ elif isinstance(values, ABCSeries):
+ return values._values
if isinstance(values, list) and dtype in [np.object_, object]:
return construct_1d_object_array_from_listlike(values)
diff --git a/pandas/tests/indexes/base_class/test_constructors.py b/pandas/tests/indexes/base_class/test_constructors.py
index fd5176a28565e..338509dd239e6 100644
--- a/pandas/tests/indexes/base_class/test_constructors.py
+++ b/pandas/tests/indexes/base_class/test_constructors.py
@@ -71,3 +71,10 @@ def test_inference_on_pandas_objects(self):
with tm.assert_produces_warning(FutureWarning, match="Dtype inference"):
result = Index(ser)
assert result.dtype != np.object_
+
+ def test_constructor_not_read_only(self):
+ # GH#57130
+ ser = Series([1, 2], dtype=object)
+ with pd.option_context("mode.copy_on_write", True):
+ idx = Index(ser)
+ assert idx._values.flags.writeable
diff --git a/pandas/tests/indexes/test_common.py b/pandas/tests/indexes/test_common.py
index 412a59d15307d..80c39322b9b81 100644
--- a/pandas/tests/indexes/test_common.py
+++ b/pandas/tests/indexes/test_common.py
@@ -500,3 +500,12 @@ def test_ndarray_compat_properties(index):
# test for validity
idx.nbytes
idx.values.nbytes
+
+
+def test_compare_read_only_array():
+ # GH#57130
+ arr = np.array([], dtype=object)
+ arr.flags.writeable = False
+ idx = pd.Index(arr)
+ result = idx > 69
+ assert result.dtype == bool
| Backport PR #57139: BUG: Index(Series) makes array read only for object dtype | https://api.github.com/repos/pandas-dev/pandas/pulls/57143 | 2024-01-30T02:17:43Z | 2024-01-30T04:37:17Z | 2024-01-30T04:37:17Z | 2024-01-30T04:37:17Z |
Backport PR #57061 on branch 2.2.x (REGR: non-unique, masked dtype index raising IndexError) | diff --git a/doc/source/whatsnew/v2.2.1.rst b/doc/source/whatsnew/v2.2.1.rst
index ff5fadec735e6..386e8cbc51f6f 100644
--- a/doc/source/whatsnew/v2.2.1.rst
+++ b/doc/source/whatsnew/v2.2.1.rst
@@ -17,6 +17,7 @@ Fixed regressions
- Fixed performance regression in :meth:`Series.combine_first` (:issue:`55845`)
- Fixed regression in :func:`merge_ordered` raising ``TypeError`` for ``fill_method="ffill"`` and ``how="left"`` (:issue:`57010`)
- Fixed regression in :func:`wide_to_long` raising an ``AttributeError`` for string columns (:issue:`57066`)
+- Fixed regression in :meth:`DataFrame.loc` raising ``IndexError`` for non-unique, masked dtype indexes where result has more than 10,000 rows (:issue:`57027`)
- Fixed regression in :meth:`DataFrameGroupBy.idxmin`, :meth:`DataFrameGroupBy.idxmax`, :meth:`SeriesGroupBy.idxmin`, :meth:`SeriesGroupBy.idxmax` ignoring the ``skipna`` argument (:issue:`57040`)
- Fixed regression in :meth:`DataFrameGroupBy.idxmin`, :meth:`DataFrameGroupBy.idxmax`, :meth:`SeriesGroupBy.idxmin`, :meth:`SeriesGroupBy.idxmax` where values containing the minimum or maximum value for the dtype could produce incorrect results (:issue:`57040`)
- Fixed regression in :meth:`Index.join` raising ``TypeError`` when joining an empty index to a non-empty index containing mixed dtype values (:issue:`57048`)
diff --git a/pandas/_libs/index.pyx b/pandas/_libs/index.pyx
index 0dc139781f58d..e4dfe9dec3623 100644
--- a/pandas/_libs/index.pyx
+++ b/pandas/_libs/index.pyx
@@ -96,6 +96,20 @@ cdef ndarray _get_bool_indexer(ndarray values, object val, ndarray mask = None):
return indexer.view(bool)
+cdef _maybe_resize_array(ndarray values, Py_ssize_t loc, Py_ssize_t max_length):
+ """
+ Resize array if loc is out of bounds.
+ """
+ cdef:
+ Py_ssize_t n = len(values)
+
+ if loc >= n:
+ while loc >= n:
+ n *= 2
+ values = np.resize(values, min(n, max_length))
+ return values
+
+
# Don't populate hash tables in monotonic indexes larger than this
_SIZE_CUTOFF = 1_000_000
@@ -450,27 +464,18 @@ cdef class IndexEngine:
# found
if val in d:
key = val
-
+ result = _maybe_resize_array(
+ result,
+ count + len(d[key]) - 1,
+ max_alloc
+ )
for j in d[key]:
-
- # realloc if needed
- if count >= n_alloc:
- n_alloc *= 2
- if n_alloc > max_alloc:
- n_alloc = max_alloc
- result = np.resize(result, n_alloc)
-
result[count] = j
count += 1
# value not found
else:
-
- if count >= n_alloc:
- n_alloc *= 2
- if n_alloc > max_alloc:
- n_alloc = max_alloc
- result = np.resize(result, n_alloc)
+ result = _maybe_resize_array(result, count, max_alloc)
result[count] = -1
count += 1
missing[count_missing] = i
@@ -1193,13 +1198,12 @@ cdef class MaskedIndexEngine(IndexEngine):
if PySequence_GetItem(target_mask, i):
if na_pos:
+ result = _maybe_resize_array(
+ result,
+ count + len(na_pos) - 1,
+ max_alloc,
+ )
for na_idx in na_pos:
- # realloc if needed
- if count >= n_alloc:
- n_alloc *= 2
- if n_alloc > max_alloc:
- n_alloc = max_alloc
-
result[count] = na_idx
count += 1
continue
@@ -1207,23 +1211,18 @@ cdef class MaskedIndexEngine(IndexEngine):
elif val in d:
# found
key = val
-
+ result = _maybe_resize_array(
+ result,
+ count + len(d[key]) - 1,
+ max_alloc,
+ )
for j in d[key]:
-
- # realloc if needed
- if count >= n_alloc:
- n_alloc *= 2
- if n_alloc > max_alloc:
- n_alloc = max_alloc
-
result[count] = j
count += 1
continue
# value not found
- if count >= n_alloc:
- n_alloc += 10_000
- result = np.resize(result, n_alloc)
+ result = _maybe_resize_array(result, count, max_alloc)
result[count] = -1
count += 1
missing[count_missing] = i
diff --git a/pandas/tests/indexing/test_loc.py b/pandas/tests/indexing/test_loc.py
index 61c44c8a2a8f4..952251a58e981 100644
--- a/pandas/tests/indexing/test_loc.py
+++ b/pandas/tests/indexing/test_loc.py
@@ -3364,3 +3364,15 @@ def test_getitem_loc_str_periodindex(self):
index = pd.period_range(start="2000", periods=20, freq="B")
series = Series(range(20), index=index)
assert series.loc["2000-01-14"] == 9
+
+ def test_loc_nonunique_masked_index(self):
+ # GH 57027
+ ids = list(range(11))
+ index = Index(ids * 1000, dtype="Int64")
+ df = DataFrame({"val": np.arange(len(index), dtype=np.intp)}, index=index)
+ result = df.loc[ids]
+ expected = DataFrame(
+ {"val": index.argsort(kind="stable").astype(np.intp)},
+ index=Index(np.array(ids).repeat(1000), dtype="Int64"),
+ )
+ tm.assert_frame_equal(result, expected)
| Backport PR #57061: REGR: non-unique, masked dtype index raising IndexError | https://api.github.com/repos/pandas-dev/pandas/pulls/57142 | 2024-01-30T02:15:35Z | 2024-01-30T04:37:02Z | 2024-01-30T04:37:02Z | 2024-01-30T04:37:02Z |
Backport PR #57102 on branch 2.2.x (ENH: Add skipna to groupby.first and groupby.last) | diff --git a/doc/source/whatsnew/v2.2.1.rst b/doc/source/whatsnew/v2.2.1.rst
index ff5fadec735e6..589903ddcca71 100644
--- a/doc/source/whatsnew/v2.2.1.rst
+++ b/doc/source/whatsnew/v2.2.1.rst
@@ -34,7 +34,8 @@ Bug fixes
Other
~~~~~
--
+- Added the argument ``skipna`` to :meth:`DataFrameGroupBy.first`, :meth:`DataFrameGroupBy.last`, :meth:`SeriesGroupBy.first`, and :meth:`SeriesGroupBy.last`; achieving ``skipna=False`` used to be available via :meth:`DataFrameGroupBy.nth`, but the behavior was changed in pandas 2.0.0 (:issue:`57019`)
+- Added the argument ``skipna`` to :meth:`Resampler.first`, :meth:`Resampler.last` (:issue:`57019`)
.. ---------------------------------------------------------------------------
.. _whatsnew_221.contributors:
diff --git a/pandas/_libs/groupby.pyi b/pandas/_libs/groupby.pyi
index 135828a23648a..a494b61fa7e3d 100644
--- a/pandas/_libs/groupby.pyi
+++ b/pandas/_libs/groupby.pyi
@@ -136,6 +136,7 @@ def group_last(
result_mask: npt.NDArray[np.bool_] | None = ...,
min_count: int = ..., # Py_ssize_t
is_datetimelike: bool = ...,
+ skipna: bool = ...,
) -> None: ...
def group_nth(
out: np.ndarray, # rank_t[:, ::1]
@@ -147,6 +148,7 @@ def group_nth(
min_count: int = ..., # int64_t
rank: int = ..., # int64_t
is_datetimelike: bool = ...,
+ skipna: bool = ...,
) -> None: ...
def group_rank(
out: np.ndarray, # float64_t[:, ::1]
diff --git a/pandas/_libs/groupby.pyx b/pandas/_libs/groupby.pyx
index ac24c0bb8df88..b855d64d0be18 100644
--- a/pandas/_libs/groupby.pyx
+++ b/pandas/_libs/groupby.pyx
@@ -1424,6 +1424,7 @@ def group_last(
uint8_t[:, ::1] result_mask=None,
Py_ssize_t min_count=-1,
bint is_datetimelike=False,
+ bint skipna=True,
) -> None:
"""
Only aggregates on axis=0
@@ -1458,14 +1459,19 @@ def group_last(
for j in range(K):
val = values[i, j]
- if uses_mask:
- isna_entry = mask[i, j]
- else:
- isna_entry = _treat_as_na(val, is_datetimelike)
+ if skipna:
+ if uses_mask:
+ isna_entry = mask[i, j]
+ else:
+ isna_entry = _treat_as_na(val, is_datetimelike)
+ if isna_entry:
+ continue
- if not isna_entry:
- nobs[lab, j] += 1
- resx[lab, j] = val
+ nobs[lab, j] += 1
+ resx[lab, j] = val
+
+ if uses_mask and not skipna:
+ result_mask[lab, j] = mask[i, j]
_check_below_mincount(
out, uses_mask, result_mask, ncounts, K, nobs, min_count, resx
@@ -1486,6 +1492,7 @@ def group_nth(
int64_t min_count=-1,
int64_t rank=1,
bint is_datetimelike=False,
+ bint skipna=True,
) -> None:
"""
Only aggregates on axis=0
@@ -1520,15 +1527,19 @@ def group_nth(
for j in range(K):
val = values[i, j]
- if uses_mask:
- isna_entry = mask[i, j]
- else:
- isna_entry = _treat_as_na(val, is_datetimelike)
+ if skipna:
+ if uses_mask:
+ isna_entry = mask[i, j]
+ else:
+ isna_entry = _treat_as_na(val, is_datetimelike)
+ if isna_entry:
+ continue
- if not isna_entry:
- nobs[lab, j] += 1
- if nobs[lab, j] == rank:
- resx[lab, j] = val
+ nobs[lab, j] += 1
+ if nobs[lab, j] == rank:
+ resx[lab, j] = val
+ if uses_mask and not skipna:
+ result_mask[lab, j] = mask[i, j]
_check_below_mincount(
out, uses_mask, result_mask, ncounts, K, nobs, min_count, resx
diff --git a/pandas/_testing/__init__.py b/pandas/_testing/__init__.py
index 672c16a85086c..361998db8e38b 100644
--- a/pandas/_testing/__init__.py
+++ b/pandas/_testing/__init__.py
@@ -236,11 +236,18 @@
+ TIMEDELTA_PYARROW_DTYPES
+ BOOL_PYARROW_DTYPES
)
+ ALL_REAL_PYARROW_DTYPES_STR_REPR = (
+ ALL_INT_PYARROW_DTYPES_STR_REPR + FLOAT_PYARROW_DTYPES_STR_REPR
+ )
else:
FLOAT_PYARROW_DTYPES_STR_REPR = []
ALL_INT_PYARROW_DTYPES_STR_REPR = []
ALL_PYARROW_DTYPES = []
+ ALL_REAL_PYARROW_DTYPES_STR_REPR = []
+ALL_REAL_NULLABLE_DTYPES = (
+ FLOAT_NUMPY_DTYPES + ALL_REAL_EXTENSION_DTYPES + ALL_REAL_PYARROW_DTYPES_STR_REPR
+)
arithmetic_dunder_methods = [
"__add__",
diff --git a/pandas/conftest.py b/pandas/conftest.py
index a11d9c2b4b2a1..7c35dfdde90ba 100644
--- a/pandas/conftest.py
+++ b/pandas/conftest.py
@@ -1642,6 +1642,38 @@ def any_numpy_dtype(request):
return request.param
+@pytest.fixture(params=tm.ALL_REAL_NULLABLE_DTYPES)
+def any_real_nullable_dtype(request):
+ """
+ Parameterized fixture for all real dtypes that can hold NA.
+
+ * float
+ * 'float32'
+ * 'float64'
+ * 'Float32'
+ * 'Float64'
+ * 'UInt8'
+ * 'UInt16'
+ * 'UInt32'
+ * 'UInt64'
+ * 'Int8'
+ * 'Int16'
+ * 'Int32'
+ * 'Int64'
+ * 'uint8[pyarrow]'
+ * 'uint16[pyarrow]'
+ * 'uint32[pyarrow]'
+ * 'uint64[pyarrow]'
+ * 'int8[pyarrow]'
+ * 'int16[pyarrow]'
+ * 'int32[pyarrow]'
+ * 'int64[pyarrow]'
+ * 'float[pyarrow]'
+ * 'double[pyarrow]'
+ """
+ return request.param
+
+
@pytest.fixture(params=tm.ALL_NUMERIC_DTYPES)
def any_numeric_dtype(request):
"""
diff --git a/pandas/core/groupby/groupby.py b/pandas/core/groupby/groupby.py
index 5b18455dbe8a8..db8949788567b 100644
--- a/pandas/core/groupby/groupby.py
+++ b/pandas/core/groupby/groupby.py
@@ -3335,9 +3335,13 @@ def max(
)
@final
- def first(self, numeric_only: bool = False, min_count: int = -1) -> NDFrameT:
+ def first(
+ self, numeric_only: bool = False, min_count: int = -1, skipna: bool = True
+ ) -> NDFrameT:
"""
- Compute the first non-null entry of each column.
+ Compute the first entry of each column within each group.
+
+ Defaults to skipping NA elements.
Parameters
----------
@@ -3345,12 +3349,17 @@ def first(self, numeric_only: bool = False, min_count: int = -1) -> NDFrameT:
Include only float, int, boolean columns.
min_count : int, default -1
The required number of valid values to perform the operation. If fewer
- than ``min_count`` non-NA values are present the result will be NA.
+ than ``min_count`` valid values are present the result will be NA.
+ skipna : bool, default True
+ Exclude NA/null values. If an entire row/column is NA, the result
+ will be NA.
+
+ .. versionadded:: 2.2.1
Returns
-------
Series or DataFrame
- First non-null of values within each group.
+ First values within each group.
See Also
--------
@@ -3402,12 +3411,17 @@ def first(x: Series):
min_count=min_count,
alias="first",
npfunc=first_compat,
+ skipna=skipna,
)
@final
- def last(self, numeric_only: bool = False, min_count: int = -1) -> NDFrameT:
+ def last(
+ self, numeric_only: bool = False, min_count: int = -1, skipna: bool = True
+ ) -> NDFrameT:
"""
- Compute the last non-null entry of each column.
+ Compute the last entry of each column within each group.
+
+ Defaults to skipping NA elements.
Parameters
----------
@@ -3416,12 +3430,17 @@ def last(self, numeric_only: bool = False, min_count: int = -1) -> NDFrameT:
everything, then use only numeric data.
min_count : int, default -1
The required number of valid values to perform the operation. If fewer
- than ``min_count`` non-NA values are present the result will be NA.
+ than ``min_count`` valid values are present the result will be NA.
+ skipna : bool, default True
+ Exclude NA/null values. If an entire row/column is NA, the result
+ will be NA.
+
+ .. versionadded:: 2.2.1
Returns
-------
Series or DataFrame
- Last non-null of values within each group.
+ Last of values within each group.
See Also
--------
@@ -3461,6 +3480,7 @@ def last(x: Series):
min_count=min_count,
alias="last",
npfunc=last_compat,
+ skipna=skipna,
)
@final
diff --git a/pandas/core/resample.py b/pandas/core/resample.py
index 3e9507bd4347f..2d430ef4dcff6 100644
--- a/pandas/core/resample.py
+++ b/pandas/core/resample.py
@@ -1306,12 +1306,15 @@ def first(
self,
numeric_only: bool = False,
min_count: int = 0,
+ skipna: bool = True,
*args,
**kwargs,
):
maybe_warn_args_and_kwargs(type(self), "first", args, kwargs)
nv.validate_resampler_func("first", args, kwargs)
- return self._downsample("first", numeric_only=numeric_only, min_count=min_count)
+ return self._downsample(
+ "first", numeric_only=numeric_only, min_count=min_count, skipna=skipna
+ )
@final
@doc(GroupBy.last)
@@ -1319,12 +1322,15 @@ def last(
self,
numeric_only: bool = False,
min_count: int = 0,
+ skipna: bool = True,
*args,
**kwargs,
):
maybe_warn_args_and_kwargs(type(self), "last", args, kwargs)
nv.validate_resampler_func("last", args, kwargs)
- return self._downsample("last", numeric_only=numeric_only, min_count=min_count)
+ return self._downsample(
+ "last", numeric_only=numeric_only, min_count=min_count, skipna=skipna
+ )
@final
@doc(GroupBy.median)
diff --git a/pandas/tests/groupby/test_reductions.py b/pandas/tests/groupby/test_reductions.py
index 422322d03c4c0..25b0f80639cff 100644
--- a/pandas/tests/groupby/test_reductions.py
+++ b/pandas/tests/groupby/test_reductions.py
@@ -7,6 +7,9 @@
from pandas._libs.tslibs import iNaT
+from pandas.core.dtypes.common import pandas_dtype
+from pandas.core.dtypes.missing import na_value_for_dtype
+
import pandas as pd
from pandas import (
DataFrame,
@@ -327,6 +330,34 @@ def test_groupby_non_arithmetic_agg_int_like_precision(method, data):
tm.assert_frame_equal(result, expected)
+@pytest.mark.parametrize("how", ["first", "last"])
+def test_first_last_skipna(any_real_nullable_dtype, sort, skipna, how):
+ # GH#57019
+ na_value = na_value_for_dtype(pandas_dtype(any_real_nullable_dtype))
+ df = DataFrame(
+ {
+ "a": [2, 1, 1, 2, 3, 3],
+ "b": [na_value, 3.0, na_value, 4.0, np.nan, np.nan],
+ "c": [na_value, 3.0, na_value, 4.0, np.nan, np.nan],
+ },
+ dtype=any_real_nullable_dtype,
+ )
+ gb = df.groupby("a", sort=sort)
+ method = getattr(gb, how)
+ result = method(skipna=skipna)
+
+ ilocs = {
+ ("first", True): [3, 1, 4],
+ ("first", False): [0, 1, 4],
+ ("last", True): [3, 1, 5],
+ ("last", False): [3, 2, 5],
+ }[how, skipna]
+ expected = df.iloc[ilocs].set_index("a")
+ if sort:
+ expected = expected.sort_index()
+ tm.assert_frame_equal(result, expected)
+
+
def test_idxmin_idxmax_axis1():
df = DataFrame(
np.random.default_rng(2).standard_normal((10, 4)), columns=["A", "B", "C", "D"]
diff --git a/pandas/tests/resample/test_base.py b/pandas/tests/resample/test_base.py
index 50644e33e45e1..dcf6c6099abab 100644
--- a/pandas/tests/resample/test_base.py
+++ b/pandas/tests/resample/test_base.py
@@ -3,6 +3,9 @@
import numpy as np
import pytest
+from pandas.core.dtypes.common import is_extension_array_dtype
+
+import pandas as pd
from pandas import (
DataFrame,
DatetimeIndex,
@@ -429,3 +432,29 @@ def test_resample_quantile(series):
result = ser.resample(freq).quantile(q)
expected = ser.resample(freq).agg(lambda x: x.quantile(q)).rename(ser.name)
tm.assert_series_equal(result, expected)
+
+
+@pytest.mark.parametrize("how", ["first", "last"])
+def test_first_last_skipna(any_real_nullable_dtype, skipna, how):
+ # GH#57019
+ if is_extension_array_dtype(any_real_nullable_dtype):
+ na_value = Series(dtype=any_real_nullable_dtype).dtype.na_value
+ else:
+ na_value = np.nan
+ df = DataFrame(
+ {
+ "a": [2, 1, 1, 2],
+ "b": [na_value, 3.0, na_value, 4.0],
+ "c": [na_value, 3.0, na_value, 4.0],
+ },
+ index=date_range("2020-01-01", periods=4, freq="D"),
+ dtype=any_real_nullable_dtype,
+ )
+ rs = df.resample("ME")
+ method = getattr(rs, how)
+ result = method(skipna=skipna)
+
+ gb = df.groupby(df.shape[0] * [pd.to_datetime("2020-01-31")])
+ expected = getattr(gb, how)(skipna=skipna)
+ expected.index.freq = "ME"
+ tm.assert_frame_equal(result, expected)
diff --git a/pandas/tests/resample/test_resample_api.py b/pandas/tests/resample/test_resample_api.py
index d3e906827b754..12abd1c98784b 100644
--- a/pandas/tests/resample/test_resample_api.py
+++ b/pandas/tests/resample/test_resample_api.py
@@ -1040,11 +1040,11 @@ def test_args_kwargs_depr(method, raises):
if raises:
with tm.assert_produces_warning(FutureWarning, match=warn_msg):
with pytest.raises(UnsupportedFunctionCall, match=error_msg):
- func(*args, 1, 2, 3)
+ func(*args, 1, 2, 3, 4)
else:
with tm.assert_produces_warning(FutureWarning, match=warn_msg):
with pytest.raises(TypeError, match=error_msg_type):
- func(*args, 1, 2, 3)
+ func(*args, 1, 2, 3, 4)
def test_df_axis_param_depr():
| Backport PR #57102: ENH: Add skipna to groupby.first and groupby.last | https://api.github.com/repos/pandas-dev/pandas/pulls/57141 | 2024-01-30T02:14:51Z | 2024-01-30T04:36:43Z | 2024-01-30T04:36:43Z | 2024-03-18T12:03:56Z |
Validate `dir` for `pd.errors` and `pd.util` | diff --git a/doc/source/reference/index.rst b/doc/source/reference/index.rst
index 7da02f7958416..7f4d05414d254 100644
--- a/doc/source/reference/index.rst
+++ b/doc/source/reference/index.rst
@@ -24,13 +24,14 @@ The following subpackages are public.
`pandas-stubs <https://github.com/pandas-dev/pandas-stubs>`_ package
which has classes in addition to those that occur in pandas for type-hinting.
-In addition, public functions in ``pandas.io`` and ``pandas.tseries`` submodules
-are mentioned in the documentation.
+In addition, public functions in ``pandas.io``, ``pandas.tseries``, ``pandas.util`` submodules
+are explicitly mentioned in the documentation. Further APIs in these modules are not guaranteed
+to be stable.
.. warning::
- The ``pandas.core``, ``pandas.compat``, and ``pandas.util`` top-level modules are PRIVATE. Stable functionality in such modules is not guaranteed.
+ The ``pandas.core``, ``pandas.compat`` top-level modules are PRIVATE. Stable functionality in such modules is not guaranteed.
.. If you update this toctree, also update the manual toctree in the
.. main index.rst.template
diff --git a/pandas/core/frame.py b/pandas/core/frame.py
index 97f4eaa7c208a..cda25bd7b98b0 100644
--- a/pandas/core/frame.py
+++ b/pandas/core/frame.py
@@ -60,6 +60,8 @@
from pandas.errors import (
ChainedAssignmentError,
InvalidIndexError,
+)
+from pandas.errors.cow import (
_chained_assignment_method_msg,
_chained_assignment_msg,
_chained_assignment_warning_method_msg,
diff --git a/pandas/core/generic.py b/pandas/core/generic.py
index 87a394761ee7c..a7e3e6c48c4ca 100644
--- a/pandas/core/generic.py
+++ b/pandas/core/generic.py
@@ -101,6 +101,8 @@
InvalidIndexError,
SettingWithCopyError,
SettingWithCopyWarning,
+)
+from pandas.errors.cow import (
_chained_assignment_method_msg,
_chained_assignment_warning_method_msg,
_check_cacher,
diff --git a/pandas/core/indexing.py b/pandas/core/indexing.py
index 2e7a237406ca5..24f3ff4279a84 100644
--- a/pandas/core/indexing.py
+++ b/pandas/core/indexing.py
@@ -27,6 +27,8 @@
IndexingError,
InvalidIndexError,
LossySetitemError,
+)
+from pandas.errors.cow import (
_chained_assignment_msg,
_chained_assignment_warning_msg,
_check_cacher,
diff --git a/pandas/core/series.py b/pandas/core/series.py
index ad63bc8a8750f..19e54954bb41c 100644
--- a/pandas/core/series.py
+++ b/pandas/core/series.py
@@ -45,6 +45,8 @@
from pandas.errors import (
ChainedAssignmentError,
InvalidIndexError,
+)
+from pandas.errors.cow import (
_chained_assignment_method_msg,
_chained_assignment_msg,
_chained_assignment_warning_method_msg,
diff --git a/pandas/errors/__init__.py b/pandas/errors/__init__.py
index d47e02cda1837..52b896dc01e8f 100644
--- a/pandas/errors/__init__.py
+++ b/pandas/errors/__init__.py
@@ -475,81 +475,6 @@ class ChainedAssignmentError(Warning):
"""
-_chained_assignment_msg = (
- "A value is trying to be set on a copy of a DataFrame or Series "
- "through chained assignment.\n"
- "When using the Copy-on-Write mode, such chained assignment never works "
- "to update the original DataFrame or Series, because the intermediate "
- "object on which we are setting values always behaves as a copy.\n\n"
- "Try using '.loc[row_indexer, col_indexer] = value' instead, to perform "
- "the assignment in a single step.\n\n"
- "See the caveats in the documentation: "
- "https://pandas.pydata.org/pandas-docs/stable/user_guide/"
- "indexing.html#returning-a-view-versus-a-copy"
-)
-
-
-_chained_assignment_method_msg = (
- "A value is trying to be set on a copy of a DataFrame or Series "
- "through chained assignment using an inplace method.\n"
- "When using the Copy-on-Write mode, such inplace method never works "
- "to update the original DataFrame or Series, because the intermediate "
- "object on which we are setting values always behaves as a copy.\n\n"
- "For example, when doing 'df[col].method(value, inplace=True)', try "
- "using 'df.method({col: value}, inplace=True)' instead, to perform "
- "the operation inplace on the original object.\n\n"
-)
-
-
-_chained_assignment_warning_msg = (
- "ChainedAssignmentError: behaviour will change in pandas 3.0!\n"
- "You are setting values through chained assignment. Currently this works "
- "in certain cases, but when using Copy-on-Write (which will become the "
- "default behaviour in pandas 3.0) this will never work to update the "
- "original DataFrame or Series, because the intermediate object on which "
- "we are setting values will behave as a copy.\n"
- "A typical example is when you are setting values in a column of a "
- "DataFrame, like:\n\n"
- 'df["col"][row_indexer] = value\n\n'
- 'Use `df.loc[row_indexer, "col"] = values` instead, to perform the '
- "assignment in a single step and ensure this keeps updating the original `df`.\n\n"
- "See the caveats in the documentation: "
- "https://pandas.pydata.org/pandas-docs/stable/user_guide/"
- "indexing.html#returning-a-view-versus-a-copy\n"
-)
-
-
-_chained_assignment_warning_method_msg = (
- "A value is trying to be set on a copy of a DataFrame or Series "
- "through chained assignment using an inplace method.\n"
- "The behavior will change in pandas 3.0. This inplace method will "
- "never work because the intermediate object on which we are setting "
- "values always behaves as a copy.\n\n"
- "For example, when doing 'df[col].method(value, inplace=True)', try "
- "using 'df.method({col: value}, inplace=True)' or "
- "df[col] = df[col].method(value) instead, to perform "
- "the operation inplace on the original object.\n\n"
-)
-
-
-def _check_cacher(obj) -> bool:
- # This is a mess, selection paths that return a view set the _cacher attribute
- # on the Series; most of them also set _item_cache which adds 1 to our relevant
- # reference count, but iloc does not, so we have to check if we are actually
- # in the item cache
- if hasattr(obj, "_cacher"):
- parent = obj._cacher[1]()
- # parent could be dead
- if parent is None:
- return False
- if hasattr(parent, "_item_cache"):
- if obj._cacher[0] in parent._item_cache:
- # Check if we are actually the item from item_cache, iloc creates a
- # new object
- return obj is parent._item_cache[obj._cacher[0]]
- return False
-
-
class NumExprClobberingError(NameError):
"""
Exception raised when trying to use a built-in numexpr name as a variable name.
@@ -831,6 +756,7 @@ class InvalidComparison(Exception):
"AbstractMethodError",
"AttributeConflictWarning",
"CategoricalConversionWarning",
+ "ChainedAssignmentError",
"ClosedFileError",
"CSSWarning",
"DatabaseError",
diff --git a/pandas/errors/cow.py b/pandas/errors/cow.py
new file mode 100644
index 0000000000000..2215ec2148757
--- /dev/null
+++ b/pandas/errors/cow.py
@@ -0,0 +1,74 @@
+from typing import Any
+
+_chained_assignment_msg = (
+ "A value is trying to be set on a copy of a DataFrame or Series "
+ "through chained assignment.\n"
+ "When using the Copy-on-Write mode, such chained assignment never works "
+ "to update the original DataFrame or Series, because the intermediate "
+ "object on which we are setting values always behaves as a copy.\n\n"
+ "Try using '.loc[row_indexer, col_indexer] = value' instead, to perform "
+ "the assignment in a single step.\n\n"
+ "See the caveats in the documentation: "
+ "https://pandas.pydata.org/pandas-docs/stable/user_guide/"
+ "indexing.html#returning-a-view-versus-a-copy"
+)
+
+
+_chained_assignment_method_msg = (
+ "A value is trying to be set on a copy of a DataFrame or Series "
+ "through chained assignment using an inplace method.\n"
+ "When using the Copy-on-Write mode, such inplace method never works "
+ "to update the original DataFrame or Series, because the intermediate "
+ "object on which we are setting values always behaves as a copy.\n\n"
+ "For example, when doing 'df[col].method(value, inplace=True)', try "
+ "using 'df.method({col: value}, inplace=True)' instead, to perform "
+ "the operation inplace on the original object.\n\n"
+)
+
+
+_chained_assignment_warning_msg = (
+ "ChainedAssignmentError: behaviour will change in pandas 3.0!\n"
+ "You are setting values through chained assignment. Currently this works "
+ "in certain cases, but when using Copy-on-Write (which will become the "
+ "default behaviour in pandas 3.0) this will never work to update the "
+ "original DataFrame or Series, because the intermediate object on which "
+ "we are setting values will behave as a copy.\n"
+ "A typical example is when you are setting values in a column of a "
+ "DataFrame, like:\n\n"
+ 'df["col"][row_indexer] = value\n\n'
+ 'Use `df.loc[row_indexer, "col"] = values` instead, to perform the '
+ "assignment in a single step and ensure this keeps updating the original `df`.\n\n"
+ "See the caveats in the documentation: "
+ "https://pandas.pydata.org/pandas-docs/stable/user_guide/"
+ "indexing.html#returning-a-view-versus-a-copy\n"
+)
+
+_chained_assignment_warning_method_msg = (
+ "A value is trying to be set on a copy of a DataFrame or Series "
+ "through chained assignment using an inplace method.\n"
+ "The behavior will change in pandas 3.0. This inplace method will "
+ "never work because the intermediate object on which we are setting "
+ "values always behaves as a copy.\n\n"
+ "For example, when doing 'df[col].method(value, inplace=True)', try "
+ "using 'df.method({col: value}, inplace=True)' or "
+ "df[col] = df[col].method(value) instead, to perform "
+ "the operation inplace on the original object.\n\n"
+)
+
+
+def _check_cacher(obj: Any) -> bool:
+ # This is a mess, selection paths that return a view set the _cacher attribute
+ # on the Series; most of them also set _item_cache which adds 1 to our relevant
+ # reference count, but iloc does not, so we have to check if we are actually
+ # in the item cache
+ if hasattr(obj, "_cacher"):
+ parent = obj._cacher[1]()
+ # parent could be dead
+ if parent is None:
+ return False
+ if hasattr(parent, "_item_cache"):
+ if obj._cacher[0] in parent._item_cache:
+ # Check if we are actually the item from item_cache, iloc creates a
+ # new object
+ return obj is parent._item_cache[obj._cacher[0]]
+ return False
diff --git a/pandas/tests/api/test_api.py b/pandas/tests/api/test_api.py
index 60bcb97aaa364..61d6aaf63adf1 100644
--- a/pandas/tests/api/test_api.py
+++ b/pandas/tests/api/test_api.py
@@ -357,6 +357,29 @@ def test_api_extensions(self):
self.check(api_extensions, self.allowed_api_extensions)
+class TestErrors(Base):
+ def test_errors(self):
+ self.check(pd.errors, pd.errors.__all__, ignored=["ctypes", "cow"])
+
+
+class TestUtil(Base):
+ def test_util(self):
+ self.check(
+ pd.util,
+ ["hash_array", "hash_pandas_object"],
+ ignored=[
+ "_decorators",
+ "_test_decorators",
+ "_exceptions",
+ "_validators",
+ "capitalize_first_letter",
+ "version",
+ "_print_versions",
+ "_tester",
+ ],
+ )
+
+
class TestTesting(Base):
funcs = [
"assert_frame_equal",
diff --git a/pandas/util/__init__.py b/pandas/util/__init__.py
index 91282fde8b11d..59ab324ba38ca 100644
--- a/pandas/util/__init__.py
+++ b/pandas/util/__init__.py
@@ -25,5 +25,9 @@ def __getattr__(key: str):
raise AttributeError(f"module 'pandas.util' has no attribute '{key}'")
+def __dir__():
+ return list(globals().keys()) + ["hash_array", "hash_pandas_object"]
+
+
def capitalize_first_letter(s: str) -> str:
return s[:1].upper() + s[1:]
| * Validates `dir(pd.errors)` matches `pd.errors.__all__` moved some items that were in the init to a new file since some of the items didn't seem to semantically belong there
* Validate `dir(pd.util)` shows the public apis in the directory. | https://api.github.com/repos/pandas-dev/pandas/pulls/57140 | 2024-01-30T02:13:20Z | 2024-01-31T19:11:45Z | 2024-01-31T19:11:45Z | 2024-01-31T19:11:48Z |
BUG: Index(Series) makes array read only for object dtype | diff --git a/doc/source/whatsnew/v2.2.1.rst b/doc/source/whatsnew/v2.2.1.rst
index ff5fadec735e6..adf79ef5e7714 100644
--- a/doc/source/whatsnew/v2.2.1.rst
+++ b/doc/source/whatsnew/v2.2.1.rst
@@ -27,7 +27,7 @@ Fixed regressions
Bug fixes
~~~~~~~~~
--
+- Fixed bug in :meth:`DataFrame.__getitem__` for empty :class:`DataFrame` with Copy-on-Write enabled (:issue:`57130`)
.. ---------------------------------------------------------------------------
.. _whatsnew_221.other:
diff --git a/pandas/_libs/ops.pyx b/pandas/_libs/ops.pyx
index 9154e836b3477..567bfc02a2950 100644
--- a/pandas/_libs/ops.pyx
+++ b/pandas/_libs/ops.pyx
@@ -29,7 +29,7 @@ from pandas._libs.util cimport is_nan
@cython.wraparound(False)
@cython.boundscheck(False)
-def scalar_compare(object[:] values, object val, object op) -> ndarray:
+def scalar_compare(ndarray[object] values, object val, object op) -> ndarray:
"""
Compare each element of `values` array with the scalar `val`, with
the comparison operation described by `op`.
diff --git a/pandas/core/common.py b/pandas/core/common.py
index 69b602feee3ea..8ae30c5257c8b 100644
--- a/pandas/core/common.py
+++ b/pandas/core/common.py
@@ -236,6 +236,8 @@ def asarray_tuplesafe(values: Iterable, dtype: NpDtype | None = None) -> ArrayLi
values = list(values)
elif isinstance(values, ABCIndex):
return values._values
+ elif isinstance(values, ABCSeries):
+ return values._values
if isinstance(values, list) and dtype in [np.object_, object]:
return construct_1d_object_array_from_listlike(values)
diff --git a/pandas/tests/indexes/base_class/test_constructors.py b/pandas/tests/indexes/base_class/test_constructors.py
index fd5176a28565e..338509dd239e6 100644
--- a/pandas/tests/indexes/base_class/test_constructors.py
+++ b/pandas/tests/indexes/base_class/test_constructors.py
@@ -71,3 +71,10 @@ def test_inference_on_pandas_objects(self):
with tm.assert_produces_warning(FutureWarning, match="Dtype inference"):
result = Index(ser)
assert result.dtype != np.object_
+
+ def test_constructor_not_read_only(self):
+ # GH#57130
+ ser = Series([1, 2], dtype=object)
+ with pd.option_context("mode.copy_on_write", True):
+ idx = Index(ser)
+ assert idx._values.flags.writeable
diff --git a/pandas/tests/indexes/test_common.py b/pandas/tests/indexes/test_common.py
index 412a59d15307d..80c39322b9b81 100644
--- a/pandas/tests/indexes/test_common.py
+++ b/pandas/tests/indexes/test_common.py
@@ -500,3 +500,12 @@ def test_ndarray_compat_properties(index):
# test for validity
idx.nbytes
idx.values.nbytes
+
+
+def test_compare_read_only_array():
+ # GH#57130
+ arr = np.array([], dtype=object)
+ arr.flags.writeable = False
+ idx = pd.Index(arr)
+ result = idx > 69
+ assert result.dtype == bool
| - [ ] closes #57130 (Replace xxxx with the GitHub issue number)
- [ ] [Tests added and passed](https://pandas.pydata.org/pandas-docs/dev/development/contributing_codebase.html#writing-tests) if fixing a bug or adding a new feature
- [ ] All [code checks passed](https://pandas.pydata.org/pandas-docs/dev/development/contributing_codebase.html#pre-commit).
- [ ] Added [type annotations](https://pandas.pydata.org/pandas-docs/dev/development/contributing_codebase.html#type-hints) to new arguments/methods/functions.
- [ ] Added an entry in the latest `doc/source/whatsnew/vX.X.X.rst` file if fixing a bug or adding a new feature.
| https://api.github.com/repos/pandas-dev/pandas/pulls/57139 | 2024-01-30T00:51:26Z | 2024-01-30T02:17:36Z | 2024-01-30T02:17:36Z | 2024-01-30T09:58:11Z |
CI: Fix numpydev tz_localize_to_utc | diff --git a/pandas/core/arrays/datetimes.py b/pandas/core/arrays/datetimes.py
index 4b804598681fa..9655ce507aade 100644
--- a/pandas/core/arrays/datetimes.py
+++ b/pandas/core/arrays/datetimes.py
@@ -933,7 +933,6 @@ def tz_convert(self, tz) -> Self:
dtype = tz_to_dtype(tz, unit=self.unit)
return self._simple_new(self._ndarray, dtype=dtype, freq=self.freq)
- @dtl.ravel_compat
def tz_localize(
self,
tz,
@@ -1089,9 +1088,17 @@ def tz_localize(
"a timedelta object"
)
+ if self.ndim == 1:
+ maybe_reshape = lambda arr: arr
+ data = self.asi8
+ else:
+ order = "F" if self._ndarray.flags.f_contiguous else "C"
+ maybe_reshape = lambda arr: arr.reshape(self.shape, order=order)
+ data = self.ravel("K").asi8
+
if self.tz is not None:
if tz is None:
- new_dates = tz_convert_from_utc(self.asi8, self.tz, reso=self._creso)
+ new_dates = tz_convert_from_utc(data, self.tz, reso=self._creso)
else:
raise TypeError("Already tz-aware, use tz_convert to convert.")
else:
@@ -1099,12 +1106,13 @@ def tz_localize(
# Convert to UTC
new_dates = tzconversion.tz_localize_to_utc(
- self.asi8,
+ data,
tz,
ambiguous=ambiguous,
nonexistent=nonexistent,
creso=self._creso,
)
+ new_dates = maybe_reshape(new_dates)
new_dates_dt64 = new_dates.view(f"M8[{self.unit}]")
dtype = tz_to_dtype(tz, unit=self.unit)
| - [ ] closes #xxxx (Replace xxxx with the GitHub issue number)
- [ ] [Tests added and passed](https://pandas.pydata.org/pandas-docs/dev/development/contributing_codebase.html#writing-tests) if fixing a bug or adding a new feature
- [ ] All [code checks passed](https://pandas.pydata.org/pandas-docs/dev/development/contributing_codebase.html#pre-commit).
- [ ] Added [type annotations](https://pandas.pydata.org/pandas-docs/dev/development/contributing_codebase.html#type-hints) to new arguments/methods/functions.
- [ ] Added an entry in the latest `doc/source/whatsnew/vX.X.X.rst` file if fixing a bug or adding a new feature.
| https://api.github.com/repos/pandas-dev/pandas/pulls/57138 | 2024-01-29T23:25:10Z | 2024-01-30T02:22:31Z | null | 2024-01-30T02:22:33Z |
DEPR: removed deprecated argument `obj` from GroupBy `get_group` | diff --git a/doc/source/whatsnew/v3.0.0.rst b/doc/source/whatsnew/v3.0.0.rst
index 6d41c29a6c614..ebcfd16f92d29 100644
--- a/doc/source/whatsnew/v3.0.0.rst
+++ b/doc/source/whatsnew/v3.0.0.rst
@@ -96,6 +96,13 @@ Deprecations
- Deprecated :meth:`Timestamp.utcnow`, use ``Timestamp.now("UTC")`` instead (:issue:`56680`)
-
+.. ---------------------------------------------------------------------------
+.. _whatsnew_300.prior_deprecations:
+
+Removal of prior version deprecations/changes
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+- Removed deprecated argument ``obj`` in :meth:`.DataFrameGroupBy.get_group` and :meth:`.SeriesGroupBy.get_group` (:issue:`53545`)
+
.. ---------------------------------------------------------------------------
.. _whatsnew_300.performance:
diff --git a/pandas/core/groupby/groupby.py b/pandas/core/groupby/groupby.py
index b2afaffc267fa..33cc3de6ac46a 100644
--- a/pandas/core/groupby/groupby.py
+++ b/pandas/core/groupby/groupby.py
@@ -1049,7 +1049,7 @@ def pipe(
return com.pipe(self, func, *args, **kwargs)
@final
- def get_group(self, name, obj=None) -> DataFrame | Series:
+ def get_group(self, name) -> DataFrame | Series:
"""
Construct DataFrame from group with provided name.
@@ -1057,19 +1057,10 @@ def get_group(self, name, obj=None) -> DataFrame | Series:
----------
name : object
The name of the group to get as a DataFrame.
- obj : DataFrame, default None
- The DataFrame to take the DataFrame out of. If
- it is None, the object groupby was called on will
- be used.
-
- .. deprecated:: 2.1.0
- The obj is deprecated and will be removed in a future version.
- Do ``df.iloc[gb.indices.get(name)]``
- instead of ``gb.get_group(name, obj=df)``.
Returns
-------
- same type as obj
+ DataFrame or Series
Examples
--------
@@ -1142,18 +1133,8 @@ def get_group(self, name, obj=None) -> DataFrame | Series:
if not len(inds):
raise KeyError(name)
- if obj is None:
- indexer = inds if self.axis == 0 else (slice(None), inds)
- return self._selected_obj.iloc[indexer]
- else:
- warnings.warn(
- "obj is deprecated and will be removed in a future version. "
- "Do ``df.iloc[gb.indices.get(name)]`` "
- "instead of ``gb.get_group(name, obj=df)``.",
- FutureWarning,
- stacklevel=find_stack_level(),
- )
- return obj._take_with_is_copy(inds, axis=self.axis)
+ indexer = inds if self.axis == 0 else (slice(None), inds)
+ return self._selected_obj.iloc[indexer]
@final
def __iter__(self) -> Iterator[tuple[Hashable, NDFrameT]]:
diff --git a/pandas/tests/groupby/test_groupby.py b/pandas/tests/groupby/test_groupby.py
index 14866ce2065ed..7f7d9544c3891 100644
--- a/pandas/tests/groupby/test_groupby.py
+++ b/pandas/tests/groupby/test_groupby.py
@@ -526,16 +526,6 @@ def test_as_index_select_column():
tm.assert_series_equal(result, expected)
-def test_obj_arg_get_group_deprecated():
- depr_msg = "obj is deprecated"
-
- df = DataFrame({"a": [1, 1, 2], "b": [3, 4, 5]})
- expected = df.iloc[df.groupby("b").indices.get(4)]
- with tm.assert_produces_warning(FutureWarning, match=depr_msg):
- result = df.groupby("b").get_group(4, obj=df)
- tm.assert_frame_equal(result, expected)
-
-
def test_groupby_as_index_select_column_sum_empty_df():
# GH 35246
df = DataFrame(columns=Index(["A", "B", "C"], name="alpha"))
| - xref #53571
- removed deprecated argument `obj` from GroupBy `get_group`
- added an entry in the doc/source/whatsnew/v3.0.0.rst
| https://api.github.com/repos/pandas-dev/pandas/pulls/57136 | 2024-01-29T22:34:05Z | 2024-01-31T03:05:17Z | 2024-01-31T03:05:17Z | 2024-01-31T03:05:33Z |
Backport PR #57122 on branch 2.2.x (CI: autouse add_doctest_imports) | diff --git a/pandas/conftest.py b/pandas/conftest.py
index 983272d79081e..a11d9c2b4b2a1 100644
--- a/pandas/conftest.py
+++ b/pandas/conftest.py
@@ -190,10 +190,6 @@ def pytest_collection_modifyitems(items, config) -> None:
if is_doctest:
for item in items:
- # autouse=True for the add_doctest_imports can lead to expensive teardowns
- # since doctest_namespace is a session fixture
- item.add_marker(pytest.mark.usefixtures("add_doctest_imports"))
-
for path, message in ignored_doctest_warnings:
ignore_doctest_warning(item, path, message)
@@ -250,7 +246,14 @@ def pytest_collection_modifyitems(items, config) -> None:
)
-@pytest.fixture
+# ----------------------------------------------------------------
+# Autouse fixtures
+# ----------------------------------------------------------------
+
+
+# https://github.com/pytest-dev/pytest/issues/11873
+# Would like to avoid autouse=True, but cannot as of pytest 8.0.0
+@pytest.fixture(autouse=True)
def add_doctest_imports(doctest_namespace) -> None:
"""
Make `np` and `pd` names available for doctests.
@@ -259,9 +262,6 @@ def add_doctest_imports(doctest_namespace) -> None:
doctest_namespace["pd"] = pd
-# ----------------------------------------------------------------
-# Autouse fixtures
-# ----------------------------------------------------------------
@pytest.fixture(autouse=True)
def configure_tests() -> None:
"""
| Backport PR #57122: CI: autouse add_doctest_imports | https://api.github.com/repos/pandas-dev/pandas/pulls/57135 | 2024-01-29T22:01:37Z | 2024-01-29T22:48:32Z | 2024-01-29T22:48:32Z | 2024-01-29T22:48:32Z |
CLN: The smallest typo correction, "engine" lacked an "n". | diff --git a/pandas/_typing.py b/pandas/_typing.py
index fa9dc14bb4bd7..0233fbbcf3a12 100644
--- a/pandas/_typing.py
+++ b/pandas/_typing.py
@@ -332,7 +332,7 @@ def readline(self) -> AnyStr_co:
@property
def closed(self) -> bool:
- # for enine=pyarrow
+ # for engine=pyarrow
...
| The title is pretty descriptive, was checking some pyarrow stuff and stumbled onto this. I apologize if a PR was not needed. Have a good one!
| https://api.github.com/repos/pandas-dev/pandas/pulls/57134 | 2024-01-29T21:18:17Z | 2024-01-29T22:14:28Z | 2024-01-29T22:14:28Z | 2024-01-29T22:16:46Z |
Backport PR #57101 on branch 2.2.x (REGR: Index.join raising TypeError when joining an empty index to a mixed type index) | diff --git a/doc/source/whatsnew/v2.2.1.rst b/doc/source/whatsnew/v2.2.1.rst
index 660594c98e0f2..ff5fadec735e6 100644
--- a/doc/source/whatsnew/v2.2.1.rst
+++ b/doc/source/whatsnew/v2.2.1.rst
@@ -19,6 +19,7 @@ Fixed regressions
- Fixed regression in :func:`wide_to_long` raising an ``AttributeError`` for string columns (:issue:`57066`)
- Fixed regression in :meth:`DataFrameGroupBy.idxmin`, :meth:`DataFrameGroupBy.idxmax`, :meth:`SeriesGroupBy.idxmin`, :meth:`SeriesGroupBy.idxmax` ignoring the ``skipna`` argument (:issue:`57040`)
- Fixed regression in :meth:`DataFrameGroupBy.idxmin`, :meth:`DataFrameGroupBy.idxmax`, :meth:`SeriesGroupBy.idxmin`, :meth:`SeriesGroupBy.idxmax` where values containing the minimum or maximum value for the dtype could produce incorrect results (:issue:`57040`)
+- Fixed regression in :meth:`Index.join` raising ``TypeError`` when joining an empty index to a non-empty index containing mixed dtype values (:issue:`57048`)
- Fixed regression in :meth:`Series.pct_change` raising a ``ValueError`` for an empty :class:`Series` (:issue:`57056`)
.. ---------------------------------------------------------------------------
diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py
index c36dcda6e2972..5e7f2e27f1275 100644
--- a/pandas/core/indexes/base.py
+++ b/pandas/core/indexes/base.py
@@ -4615,38 +4615,12 @@ def join(
if level is not None and (self._is_multi or other._is_multi):
return self._join_level(other, level, how=how)
- lidx: np.ndarray | None
- ridx: np.ndarray | None
-
- if len(other) == 0:
- if how in ("left", "outer"):
- if sort and not self.is_monotonic_increasing:
- lidx = self.argsort()
- join_index = self.take(lidx)
- else:
- lidx = None
- join_index = self._view()
- ridx = np.broadcast_to(np.intp(-1), len(join_index))
- return join_index, lidx, ridx
- elif how in ("right", "inner", "cross"):
- join_index = other._view()
- lidx = np.array([], dtype=np.intp)
- return join_index, lidx, None
-
- if len(self) == 0:
- if how in ("right", "outer"):
- if sort and not other.is_monotonic_increasing:
- ridx = other.argsort()
- join_index = other.take(ridx)
- else:
- ridx = None
- join_index = other._view()
- lidx = np.broadcast_to(np.intp(-1), len(join_index))
- return join_index, lidx, ridx
- elif how in ("left", "inner", "cross"):
- join_index = self._view()
- ridx = np.array([], dtype=np.intp)
- return join_index, None, ridx
+ if len(self) == 0 or len(other) == 0:
+ try:
+ return self._join_empty(other, how, sort)
+ except TypeError:
+ # object dtype; non-comparable objects
+ pass
if self.dtype != other.dtype:
dtype = self._find_common_type_compat(other)
@@ -4681,6 +4655,33 @@ def join(
return self._join_via_get_indexer(other, how, sort)
+ @final
+ def _join_empty(
+ self, other: Index, how: JoinHow, sort: bool
+ ) -> tuple[Index, npt.NDArray[np.intp] | None, npt.NDArray[np.intp] | None]:
+ assert len(self) == 0 or len(other) == 0
+ _validate_join_method(how)
+
+ lidx: np.ndarray | None
+ ridx: np.ndarray | None
+
+ if len(other):
+ how = cast(JoinHow, {"left": "right", "right": "left"}.get(how, how))
+ join_index, ridx, lidx = other._join_empty(self, how, sort)
+ elif how in ["left", "outer"]:
+ if sort and not self.is_monotonic_increasing:
+ lidx = self.argsort()
+ join_index = self.take(lidx)
+ else:
+ lidx = None
+ join_index = self._view()
+ ridx = np.broadcast_to(np.intp(-1), len(join_index))
+ else:
+ join_index = other._view()
+ lidx = np.array([], dtype=np.intp)
+ ridx = None
+ return join_index, lidx, ridx
+
@final
def _join_via_get_indexer(
self, other: Index, how: JoinHow, sort: bool
diff --git a/pandas/tests/reshape/merge/test_join.py b/pandas/tests/reshape/merge/test_join.py
index 9a2f18f33bce5..db5a0437a14f0 100644
--- a/pandas/tests/reshape/merge/test_join.py
+++ b/pandas/tests/reshape/merge/test_join.py
@@ -1042,6 +1042,25 @@ def test_join_empty(left_empty, how, exp):
tm.assert_frame_equal(result, expected)
+def test_join_empty_uncomparable_columns():
+ # GH 57048
+ df1 = DataFrame()
+ df2 = DataFrame(columns=["test"])
+ df3 = DataFrame(columns=["foo", ("bar", "baz")])
+
+ result = df1 + df2
+ expected = DataFrame(columns=["test"])
+ tm.assert_frame_equal(result, expected)
+
+ result = df2 + df3
+ expected = DataFrame(columns=[("bar", "baz"), "foo", "test"])
+ tm.assert_frame_equal(result, expected)
+
+ result = df1 + df3
+ expected = DataFrame(columns=[("bar", "baz"), "foo"])
+ tm.assert_frame_equal(result, expected)
+
+
@pytest.mark.parametrize(
"how, values",
[
| Backport PR #57101: REGR: Index.join raising TypeError when joining an empty index to a mixed type index | https://api.github.com/repos/pandas-dev/pandas/pulls/57133 | 2024-01-29T19:53:49Z | 2024-01-29T20:53:49Z | 2024-01-29T20:53:49Z | 2024-01-29T20:53:50Z |
Backport PR #57126 on branch 2.2.x (Bump pypa/cibuildwheel from 2.16.2 to 2.16.4) | diff --git a/.github/workflows/wheels.yml b/.github/workflows/wheels.yml
index 841559c8e9799..6d3b9048a2122 100644
--- a/.github/workflows/wheels.yml
+++ b/.github/workflows/wheels.yml
@@ -139,7 +139,7 @@ jobs:
- name: Build normal wheels
if: ${{ (env.IS_SCHEDULE_DISPATCH != 'true' || env.IS_PUSH == 'true') }}
- uses: pypa/cibuildwheel@v2.16.2
+ uses: pypa/cibuildwheel@v2.16.4
with:
package-dir: ./dist/${{ matrix.buildplat[1] == 'macosx_*' && env.sdist_name || needs.build_sdist.outputs.sdist_file }}
env:
@@ -148,7 +148,7 @@ jobs:
- name: Build nightly wheels (with NumPy pre-release)
if: ${{ (env.IS_SCHEDULE_DISPATCH == 'true' && env.IS_PUSH != 'true') }}
- uses: pypa/cibuildwheel@v2.16.2
+ uses: pypa/cibuildwheel@v2.16.4
with:
package-dir: ./dist/${{ matrix.buildplat[1] == 'macosx_*' && env.sdist_name || needs.build_sdist.outputs.sdist_file }}
env:
| Backport PR #57126: Bump pypa/cibuildwheel from 2.16.2 to 2.16.4 | https://api.github.com/repos/pandas-dev/pandas/pulls/57132 | 2024-01-29T19:21:11Z | 2024-01-29T20:53:39Z | 2024-01-29T20:53:39Z | 2024-01-29T20:53:39Z |
Fix typo in missing_data.rst | diff --git a/doc/source/user_guide/missing_data.rst b/doc/source/user_guide/missing_data.rst
index 4a2aa565dd15c..7ee77b7648486 100644
--- a/doc/source/user_guide/missing_data.rst
+++ b/doc/source/user_guide/missing_data.rst
@@ -30,7 +30,7 @@ use :class:`api.types.NaTType`.
pd.Series(["2020", "2020"], dtype=pd.PeriodDtype("D")).reindex([0, 1, 2])
:class:`NA` for :class:`StringDtype`, :class:`Int64Dtype` (and other bit widths),
-:class:`Float64Dtype`(and other bit widths), :class:`BooleanDtype` and :class:`ArrowDtype`.
+:class:`Float64Dtype` (and other bit widths), :class:`BooleanDtype` and :class:`ArrowDtype`.
These types will maintain the original data type of the data.
For typing applications, use :class:`api.types.NAType`.
| <img width="736" alt="Screen Shot 2024-01-29 at 10 58 11 AM" src="https://github.com/pandas-dev/pandas/assets/48232/1c2b9c87-9acc-43a6-8fe2-19a3a131c9c1">
| https://api.github.com/repos/pandas-dev/pandas/pulls/57131 | 2024-01-29T17:58:38Z | 2024-01-29T19:18:09Z | 2024-01-29T19:18:09Z | 2024-01-29T19:18:15Z |
Try to enable some aspects of LSAN | diff --git a/.github/workflows/unit-tests.yml b/.github/workflows/unit-tests.yml
index a3cffb4b03b93..10c21613403f4 100644
--- a/.github/workflows/unit-tests.yml
+++ b/.github/workflows/unit-tests.yml
@@ -102,7 +102,7 @@ jobs:
- name: "ASAN / UBSAN"
env_file: actions-311-sanitizers.yaml
pattern: "not slow and not network and not single_cpu and not skip_ubsan"
- asan_options: "ASAN_OPTIONS=detect_leaks=0"
+ asan_options: LSAN_OPTIONS=suppressions=$(pwd)/lsan_suppress.txt
preload: LD_PRELOAD=$(gcc -print-file-name=libasan.so)
meson_args: --config-settings=setup-args="-Db_sanitize=address,undefined"
cflags_adds: -fno-sanitize-recover=all
diff --git a/lsan_suppress.txt b/lsan_suppress.txt
new file mode 100644
index 0000000000000..be28a21019dad
--- /dev/null
+++ b/lsan_suppress.txt
@@ -0,0 +1,15 @@
+leak:obmalloc.c
+leak:stringio.c
+leak:pyo3
+leak:crypto
+leak:unicodeobject.c
+leak:bytesobject.c
+leak:listobject.c
+leak:gcmodule.c
+leak:PyThread
+leak:arrow
+leak:numpy
+leak:Qt
+leak:libxml2
+leak:libxslt
+leak:HashTable
| null | https://api.github.com/repos/pandas-dev/pandas/pulls/57129 | 2024-01-29T16:39:30Z | 2024-03-19T20:12:47Z | null | 2024-03-19T20:12:48Z |
CI: avoid DeprecationWarning in validate_min_versions_in_sync script | diff --git a/scripts/validate_min_versions_in_sync.py b/scripts/validate_min_versions_in_sync.py
index 7dd3e96e6ec18..f8cce255f532d 100755
--- a/scripts/validate_min_versions_in_sync.py
+++ b/scripts/validate_min_versions_in_sync.py
@@ -143,7 +143,7 @@ def clean_version_list(
yaml_versions: list[str], toml_version: version.Version
) -> list[str]:
for i in range(len(yaml_versions)):
- yaml_version = yaml_versions[i]
+ yaml_version = yaml_versions[i].strip()
operator = get_operator_from(yaml_version)
assert operator is not None
if "<=" in operator or ">=" in operator:
| this warning is showing up in CI because of the extra whitespace here
https://github.com/pandas-dev/pandas/blob/09cb36710aab9d9a724602aa7ccfde33fbc51f63/scripts/tests/data/deps_expected_random.yaml#L41
which then goes to the `except` arm in
https://github.com/pandas-dev/pandas/blob/ffc583966451fd26cdd4c350018362f6fadd02a3/pandas/util/version/__init__.py#L122-L125
which throws the warning | https://api.github.com/repos/pandas-dev/pandas/pulls/57127 | 2024-01-29T08:58:27Z | 2024-01-31T18:25:24Z | 2024-01-31T18:25:24Z | 2024-01-31T18:25:36Z |
Bump pypa/cibuildwheel from 2.16.2 to 2.16.4 | diff --git a/.github/workflows/wheels.yml b/.github/workflows/wheels.yml
index 841559c8e9799..6d3b9048a2122 100644
--- a/.github/workflows/wheels.yml
+++ b/.github/workflows/wheels.yml
@@ -139,7 +139,7 @@ jobs:
- name: Build normal wheels
if: ${{ (env.IS_SCHEDULE_DISPATCH != 'true' || env.IS_PUSH == 'true') }}
- uses: pypa/cibuildwheel@v2.16.2
+ uses: pypa/cibuildwheel@v2.16.4
with:
package-dir: ./dist/${{ matrix.buildplat[1] == 'macosx_*' && env.sdist_name || needs.build_sdist.outputs.sdist_file }}
env:
@@ -148,7 +148,7 @@ jobs:
- name: Build nightly wheels (with NumPy pre-release)
if: ${{ (env.IS_SCHEDULE_DISPATCH == 'true' && env.IS_PUSH != 'true') }}
- uses: pypa/cibuildwheel@v2.16.2
+ uses: pypa/cibuildwheel@v2.16.4
with:
package-dir: ./dist/${{ matrix.buildplat[1] == 'macosx_*' && env.sdist_name || needs.build_sdist.outputs.sdist_file }}
env:
| Bumps [pypa/cibuildwheel](https://github.com/pypa/cibuildwheel) from 2.16.2 to 2.16.4.
<details>
<summary>Release notes</summary>
<p><em>Sourced from <a href="https://github.com/pypa/cibuildwheel/releases">pypa/cibuildwheel's releases</a>.</em></p>
<blockquote>
<h2>v2.16.4</h2>
<p>🛠 Update manylinux pins to upgrade from a problematic PyPy version. (<a href="https://redirect.github.com/pypa/cibuildwheel/issues/1737">#1737</a>)</p>
<h2>v2.16.3</h2>
<ul>
<li>🐛 Fix a bug when building from sdist, where relative paths to files in the package didn't work because the working directory was wrong (<a href="https://redirect.github.com/pypa/cibuildwheel/issues/1687">#1687</a>)</li>
<li>🛠 Adds the ability to disable mounting the host filesystem in containers to <code>/host</code>, through the <code>disable_host_mount</code> suboption on <a href="https://cibuildwheel.readthedocs.io/en/stable/options/#container-engine"><code>CIBW_CONTAINER_ENGINE</code></a>.</li>
<li>📚 A lot of docs improvements! (<a href="https://redirect.github.com/pypa/cibuildwheel/issues/1708">#1708</a>, <a href="https://redirect.github.com/pypa/cibuildwheel/issues/1705">#1705</a>, <a href="https://redirect.github.com/pypa/cibuildwheel/issues/1686">#1686</a>, <a href="https://redirect.github.com/pypa/cibuildwheel/issues/1679">#1679</a>, <a href="https://redirect.github.com/pypa/cibuildwheel/issues/1667">#1667</a>, <a href="https://redirect.github.com/pypa/cibuildwheel/issues/1665">#1665</a>)</li>
</ul>
</blockquote>
</details>
<details>
<summary>Changelog</summary>
<p><em>Sourced from <a href="https://github.com/pypa/cibuildwheel/blob/main/docs/changelog.md">pypa/cibuildwheel's changelog</a>.</em></p>
<blockquote>
<h3>v2.16.4</h3>
<p><em>28 January 2024</em></p>
<ul>
<li>🛠 Update manylinux pins to upgrade from a problematic PyPy version. (<a href="https://redirect.github.com/pypa/cibuildwheel/issues/1737">#1737</a>)</li>
</ul>
<h3>v2.16.3</h3>
<p><em>26 January 2024</em></p>
<ul>
<li>🐛 Fix a bug when building from sdist, where relative paths to files in the package didn't work because the working directory was wrong (<a href="https://redirect.github.com/pypa/cibuildwheel/issues/1687">#1687</a>)</li>
<li>🛠 Adds the ability to disable mounting the host filesystem in containers to <code>/host</code>, through the <code>disable_host_mount</code> suboption on <a href="https://cibuildwheel.readthedocs.io/en/stable/options/#container-engine"><code>CIBW_CONTAINER_ENGINE</code></a>.</li>
<li>📚 A lot of docs improvements! (<a href="https://redirect.github.com/pypa/cibuildwheel/issues/1708">#1708</a>, <a href="https://redirect.github.com/pypa/cibuildwheel/issues/1705">#1705</a>, <a href="https://redirect.github.com/pypa/cibuildwheel/issues/1686">#1686</a>, <a href="https://redirect.github.com/pypa/cibuildwheel/issues/1679">#1679</a>, <a href="https://redirect.github.com/pypa/cibuildwheel/issues/1667">#1667</a>, <a href="https://redirect.github.com/pypa/cibuildwheel/issues/1665">#1665</a>)</li>
</ul>
</blockquote>
</details>
<details>
<summary>Commits</summary>
<ul>
<li><a href="https://github.com/pypa/cibuildwheel/commit/0b04ab1040366101259658b355777e4ff2d16f83"><code>0b04ab1</code></a> Bump version: v2.16.4</li>
<li><a href="https://github.com/pypa/cibuildwheel/commit/34b049f1389fd9cfef5de1d1781ec67759770eea"><code>34b049f</code></a> [Bot] Update dependencies (<a href="https://redirect.github.com/pypa/cibuildwheel/issues/1737">#1737</a>)</li>
<li><a href="https://github.com/pypa/cibuildwheel/commit/4a79413fe6d6fd88b4317bcf0e80252292fd34f5"><code>4a79413</code></a> Remove the Cirrus CI badge from readme</li>
<li><a href="https://github.com/pypa/cibuildwheel/commit/e250df5d5da8c45226a8de1a80e6bfbbf46f5e4b"><code>e250df5</code></a> Bump version: v2.16.3</li>
<li><a href="https://github.com/pypa/cibuildwheel/commit/fd0aae31e315073be9015f63972bd30f6f0fb80c"><code>fd0aae3</code></a> Merge pull request <a href="https://redirect.github.com/pypa/cibuildwheel/issues/1686">#1686</a> from doronz88/refactor/doc-linux-clarification</li>
<li><a href="https://github.com/pypa/cibuildwheel/commit/2a83588552e256468770b630f6d7cc4e798a5d00"><code>2a83588</code></a> Apply suggestions from code review</li>
<li><a href="https://github.com/pypa/cibuildwheel/commit/a1e3efb81c738c54142149bf8e04a7818645e279"><code>a1e3efb</code></a> fix: correct path when building sdist package (<a href="https://redirect.github.com/pypa/cibuildwheel/issues/1687">#1687</a>)</li>
<li><a href="https://github.com/pypa/cibuildwheel/commit/c0e4b16ab9dde5dab19f7b6cf2d21e13f58058f0"><code>c0e4b16</code></a> [Bot] Update dependencies (<a href="https://redirect.github.com/pypa/cibuildwheel/issues/1728">#1728</a>)</li>
<li><a href="https://github.com/pypa/cibuildwheel/commit/40b63b834dd311eb4712ef784a5dfc63f8305d4f"><code>40b63b8</code></a> [pre-commit.ci] pre-commit autoupdate (<a href="https://redirect.github.com/pypa/cibuildwheel/issues/1729">#1729</a>)</li>
<li><a href="https://github.com/pypa/cibuildwheel/commit/23a6e88b0cc95cf581a8304293950a60939ce002"><code>23a6e88</code></a> Merge pull request <a href="https://redirect.github.com/pypa/cibuildwheel/issues/1672">#1672</a> from pypa/disable-host-mount</li>
<li>Additional commits viewable in <a href="https://github.com/pypa/cibuildwheel/compare/v2.16.2...v2.16.4">compare view</a></li>
</ul>
</details>
<br />
[](https://docs.github.com/en/github/managing-security-vulnerabilities/about-dependabot-security-updates#about-compatibility-scores)
Dependabot will resolve any conflicts with this PR as long as you don't alter it yourself. You can also trigger a rebase manually by commenting `@dependabot rebase`.
[//]: # (dependabot-automerge-start)
[//]: # (dependabot-automerge-end)
---
<details>
<summary>Dependabot commands and options</summary>
<br />
You can trigger Dependabot actions by commenting on this PR:
- `@dependabot rebase` will rebase this PR
- `@dependabot recreate` will recreate this PR, overwriting any edits that have been made to it
- `@dependabot merge` will merge this PR after your CI passes on it
- `@dependabot squash and merge` will squash and merge this PR after your CI passes on it
- `@dependabot cancel merge` will cancel a previously requested merge and block automerging
- `@dependabot reopen` will reopen this PR if it is closed
- `@dependabot close` will close this PR and stop Dependabot recreating it. You can achieve the same result by closing it manually
- `@dependabot show <dependency name> ignore conditions` will show all of the ignore conditions of the specified dependency
- `@dependabot ignore this major version` will close this PR and stop Dependabot creating any more for this major version (unless you reopen the PR or upgrade to it yourself)
- `@dependabot ignore this minor version` will close this PR and stop Dependabot creating any more for this minor version (unless you reopen the PR or upgrade to it yourself)
- `@dependabot ignore this dependency` will close this PR and stop Dependabot creating any more for this dependency (unless you reopen the PR or upgrade to it yourself)
</details> | https://api.github.com/repos/pandas-dev/pandas/pulls/57126 | 2024-01-29T08:21:01Z | 2024-01-29T19:20:45Z | 2024-01-29T19:20:45Z | 2024-01-29T19:20:48Z |
CI: autouse add_doctest_imports | diff --git a/pandas/conftest.py b/pandas/conftest.py
index 94805313ccfc1..c70edeb6ad8ec 100644
--- a/pandas/conftest.py
+++ b/pandas/conftest.py
@@ -190,10 +190,6 @@ def pytest_collection_modifyitems(items, config) -> None:
if is_doctest:
for item in items:
- # autouse=True for the add_doctest_imports can lead to expensive teardowns
- # since doctest_namespace is a session fixture
- item.add_marker(pytest.mark.usefixtures("add_doctest_imports"))
-
for path, message in ignored_doctest_warnings:
ignore_doctest_warning(item, path, message)
@@ -250,7 +246,14 @@ def pytest_collection_modifyitems(items, config) -> None:
)
-@pytest.fixture
+# ----------------------------------------------------------------
+# Autouse fixtures
+# ----------------------------------------------------------------
+
+
+# https://github.com/pytest-dev/pytest/issues/11873
+# Would like to avoid autouse=True, but cannot as of pytest 8.0.0
+@pytest.fixture(autouse=True)
def add_doctest_imports(doctest_namespace) -> None:
"""
Make `np` and `pd` names available for doctests.
@@ -259,9 +262,6 @@ def add_doctest_imports(doctest_namespace) -> None:
doctest_namespace["pd"] = pd
-# ----------------------------------------------------------------
-# Autouse fixtures
-# ----------------------------------------------------------------
@pytest.fixture(autouse=True)
def configure_tests() -> None:
"""
| - [ ] closes #xxxx (Replace xxxx with the GitHub issue number)
- [ ] [Tests added and passed](https://pandas.pydata.org/pandas-docs/dev/development/contributing_codebase.html#writing-tests) if fixing a bug or adding a new feature
- [ ] All [code checks passed](https://pandas.pydata.org/pandas-docs/dev/development/contributing_codebase.html#pre-commit).
- [ ] Added [type annotations](https://pandas.pydata.org/pandas-docs/dev/development/contributing_codebase.html#type-hints) to new arguments/methods/functions.
- [ ] Added an entry in the latest `doc/source/whatsnew/vX.X.X.rst` file if fixing a bug or adding a new feature.
Just trying this out to see impact to the CI
Ref: https://github.com/pytest-dev/pytest/issues/11873
Effectively reverts #45667 | https://api.github.com/repos/pandas-dev/pandas/pulls/57122 | 2024-01-29T03:53:57Z | 2024-01-29T22:01:29Z | 2024-01-29T22:01:29Z | 2024-01-29T22:37:37Z |
REGR: Fix to_numpy for masked array with non-numeric dtype | diff --git a/doc/source/whatsnew/v2.2.1.rst b/doc/source/whatsnew/v2.2.1.rst
index 883627bd4b19b..009d4794dbd1d 100644
--- a/doc/source/whatsnew/v2.2.1.rst
+++ b/doc/source/whatsnew/v2.2.1.rst
@@ -17,12 +17,15 @@ Fixed regressions
- Fixed performance regression in :meth:`Series.combine_first` (:issue:`55845`)
- Fixed regression in :func:`merge_ordered` raising ``TypeError`` for ``fill_method="ffill"`` and ``how="left"`` (:issue:`57010`)
- Fixed regression in :func:`wide_to_long` raising an ``AttributeError`` for string columns (:issue:`57066`)
+- Fixed regression in :meth:`.DataFrameGroupBy.idxmin`, :meth:`.DataFrameGroupBy.idxmax`, :meth:`.SeriesGroupBy.idxmin`, :meth:`.SeriesGroupBy.idxmax` ignoring the ``skipna`` argument (:issue:`57040`)
+- Fixed regression in :meth:`.DataFrameGroupBy.idxmin`, :meth:`.DataFrameGroupBy.idxmax`, :meth:`.SeriesGroupBy.idxmin`, :meth:`.SeriesGroupBy.idxmax` where values containing the minimum or maximum value for the dtype could produce incorrect results (:issue:`57040`)
- Fixed regression in :meth:`DataFrame.loc` raising ``IndexError`` for non-unique, masked dtype indexes where result has more than 10,000 rows (:issue:`57027`)
- Fixed regression in :meth:`DataFrame.sort_index` not producing a stable sort for a index with duplicates (:issue:`57151`)
- Fixed regression in :meth:`DataFrame.to_dict` with ``orient='list'`` and datetime or timedelta types returning integers (:issue:`54824`)
- Fixed regression in :meth:`DataFrame.to_json` converting nullable integers to floats (:issue:`57224`)
- Fixed regression in :meth:`DataFrameGroupBy.idxmin`, :meth:`DataFrameGroupBy.idxmax`, :meth:`SeriesGroupBy.idxmin`, :meth:`SeriesGroupBy.idxmax` ignoring the ``skipna`` argument (:issue:`57040`)
- Fixed regression in :meth:`DataFrameGroupBy.idxmin`, :meth:`DataFrameGroupBy.idxmax`, :meth:`SeriesGroupBy.idxmin`, :meth:`SeriesGroupBy.idxmax` where values containing the minimum or maximum value for the dtype could produce incorrect results (:issue:`57040`)
+- Fixed regression in :meth:`ExtensionArray.to_numpy` raising for non-numeric masked dtypes (:issue:`56991`)
- Fixed regression in :meth:`Index.join` raising ``TypeError`` when joining an empty index to a non-empty index containing mixed dtype values (:issue:`57048`)
- Fixed regression in :meth:`Series.pct_change` raising a ``ValueError`` for an empty :class:`Series` (:issue:`57056`)
- Fixed regression in :meth:`Series.to_numpy` when dtype is given as float and the data contains NaNs (:issue:`57121`)
diff --git a/pandas/core/arrays/masked.py b/pandas/core/arrays/masked.py
index f04c50251f19e..302ce996fb4f2 100644
--- a/pandas/core/arrays/masked.py
+++ b/pandas/core/arrays/masked.py
@@ -502,6 +502,8 @@ def to_numpy(
"""
hasna = self._hasna
dtype, na_value = to_numpy_dtype_inference(self, dtype, na_value, hasna)
+ if dtype is None:
+ dtype = object
if hasna:
if (
diff --git a/pandas/tests/arrays/masked/test_function.py b/pandas/tests/arrays/masked/test_function.py
index d5ea60ecb754d..b4b1761217826 100644
--- a/pandas/tests/arrays/masked/test_function.py
+++ b/pandas/tests/arrays/masked/test_function.py
@@ -5,6 +5,7 @@
import pandas as pd
import pandas._testing as tm
+from pandas.core.arrays import BaseMaskedArray
arrays = [pd.array([1, 2, 3, None], dtype=dtype) for dtype in tm.ALL_INT_EA_DTYPES]
arrays += [
@@ -55,3 +56,19 @@ def test_tolist(data):
result = data.tolist()
expected = list(data)
tm.assert_equal(result, expected)
+
+
+def test_to_numpy():
+ # GH#56991
+
+ class MyStringArray(BaseMaskedArray):
+ dtype = pd.StringDtype()
+ _dtype_cls = pd.StringDtype
+ _internal_fill_value = pd.NA
+
+ arr = MyStringArray(
+ values=np.array(["a", "b", "c"]), mask=np.array([False, True, False])
+ )
+ result = arr.to_numpy()
+ expected = np.array(["a", pd.NA, "c"])
+ tm.assert_numpy_array_equal(result, expected)
| - [ ] closes #56991 (Replace xxxx with the GitHub issue number)
- [ ] [Tests added and passed](https://pandas.pydata.org/pandas-docs/dev/development/contributing_codebase.html#writing-tests) if fixing a bug or adding a new feature
- [ ] All [code checks passed](https://pandas.pydata.org/pandas-docs/dev/development/contributing_codebase.html#pre-commit).
- [ ] Added [type annotations](https://pandas.pydata.org/pandas-docs/dev/development/contributing_codebase.html#type-hints) to new arguments/methods/functions.
- [ ] Added an entry in the latest `doc/source/whatsnew/vX.X.X.rst` file if fixing a bug or adding a new feature.
| https://api.github.com/repos/pandas-dev/pandas/pulls/57121 | 2024-01-29T00:01:05Z | 2024-02-09T22:04:56Z | 2024-02-09T22:04:56Z | 2024-02-09T22:05:15Z |
Backport PR #57089 on branch 2.2.x (BUG: wide_to_long with string columns) | diff --git a/doc/source/whatsnew/v2.2.1.rst b/doc/source/whatsnew/v2.2.1.rst
index b9b2821ebc468..660594c98e0f2 100644
--- a/doc/source/whatsnew/v2.2.1.rst
+++ b/doc/source/whatsnew/v2.2.1.rst
@@ -16,6 +16,7 @@ Fixed regressions
- Fixed memory leak in :func:`read_csv` (:issue:`57039`)
- Fixed performance regression in :meth:`Series.combine_first` (:issue:`55845`)
- Fixed regression in :func:`merge_ordered` raising ``TypeError`` for ``fill_method="ffill"`` and ``how="left"`` (:issue:`57010`)
+- Fixed regression in :func:`wide_to_long` raising an ``AttributeError`` for string columns (:issue:`57066`)
- Fixed regression in :meth:`DataFrameGroupBy.idxmin`, :meth:`DataFrameGroupBy.idxmax`, :meth:`SeriesGroupBy.idxmin`, :meth:`SeriesGroupBy.idxmax` ignoring the ``skipna`` argument (:issue:`57040`)
- Fixed regression in :meth:`DataFrameGroupBy.idxmin`, :meth:`DataFrameGroupBy.idxmax`, :meth:`SeriesGroupBy.idxmin`, :meth:`SeriesGroupBy.idxmax` where values containing the minimum or maximum value for the dtype could produce incorrect results (:issue:`57040`)
- Fixed regression in :meth:`Series.pct_change` raising a ``ValueError`` for an empty :class:`Series` (:issue:`57056`)
diff --git a/pandas/core/reshape/melt.py b/pandas/core/reshape/melt.py
index bb1cd0d738dac..e54f847895f1a 100644
--- a/pandas/core/reshape/melt.py
+++ b/pandas/core/reshape/melt.py
@@ -458,8 +458,7 @@ def wide_to_long(
def get_var_names(df, stub: str, sep: str, suffix: str):
regex = rf"^{re.escape(stub)}{re.escape(sep)}{suffix}$"
- pattern = re.compile(regex)
- return df.columns[df.columns.str.match(pattern)]
+ return df.columns[df.columns.str.match(regex)]
def melt_stub(df, stub: str, i, j, value_vars, sep: str):
newdf = melt(
diff --git a/pandas/core/strings/accessor.py b/pandas/core/strings/accessor.py
index 1b7d632c0fa80..da10a12d02ae4 100644
--- a/pandas/core/strings/accessor.py
+++ b/pandas/core/strings/accessor.py
@@ -1336,14 +1336,14 @@ def contains(
return self._wrap_result(result, fill_value=na, returns_string=False)
@forbid_nonstring_types(["bytes"])
- def match(self, pat, case: bool = True, flags: int = 0, na=None):
+ def match(self, pat: str, case: bool = True, flags: int = 0, na=None):
"""
Determine if each string starts with a match of a regular expression.
Parameters
----------
pat : str
- Character sequence or regular expression.
+ Character sequence.
case : bool, default True
If True, case sensitive.
flags : int, default 0 (no flags)
diff --git a/pandas/tests/reshape/test_melt.py b/pandas/tests/reshape/test_melt.py
index ff9f927597956..272c5b3403293 100644
--- a/pandas/tests/reshape/test_melt.py
+++ b/pandas/tests/reshape/test_melt.py
@@ -1220,3 +1220,33 @@ def test_missing_stubname(self, dtype):
new_level = expected.index.levels[0].astype(dtype)
expected.index = expected.index.set_levels(new_level, level=0)
tm.assert_frame_equal(result, expected)
+
+
+def test_wide_to_long_pyarrow_string_columns():
+ # GH 57066
+ pytest.importorskip("pyarrow")
+ df = DataFrame(
+ {
+ "ID": {0: 1},
+ "R_test1": {0: 1},
+ "R_test2": {0: 1},
+ "R_test3": {0: 2},
+ "D": {0: 1},
+ }
+ )
+ df.columns = df.columns.astype("string[pyarrow_numpy]")
+ result = wide_to_long(
+ df, stubnames="R", i="ID", j="UNPIVOTED", sep="_", suffix=".*"
+ )
+ expected = DataFrame(
+ [[1, 1], [1, 1], [1, 2]],
+ columns=Index(["D", "R"], dtype=object),
+ index=pd.MultiIndex.from_arrays(
+ [
+ [1, 1, 1],
+ Index(["test1", "test2", "test3"], dtype="string[pyarrow_numpy]"),
+ ],
+ names=["ID", "UNPIVOTED"],
+ ),
+ )
+ tm.assert_frame_equal(result, expected)
| Backport PR #57089: BUG: wide_to_long with string columns | https://api.github.com/repos/pandas-dev/pandas/pulls/57120 | 2024-01-28T23:55:41Z | 2024-01-29T03:44:40Z | 2024-01-29T03:44:40Z | 2024-01-29T03:44:40Z |
DEPR: Enforce deprecation of ArrayManager | diff --git a/asv_bench/benchmarks/frame_methods.py b/asv_bench/benchmarks/frame_methods.py
index a283afd1f0f1e..eead38dffaff4 100644
--- a/asv_bench/benchmarks/frame_methods.py
+++ b/asv_bench/benchmarks/frame_methods.py
@@ -593,8 +593,6 @@ def setup(self):
N = 10000
# this is the worst case, where every column has NaNs.
arr = np.random.randn(N, 100)
- # NB: we need to set values in array, not in df.values, otherwise
- # the benchmark will be misleading for ArrayManager
arr[::2] = np.nan
self.df = DataFrame(arr)
diff --git a/doc/source/whatsnew/v3.0.0.rst b/doc/source/whatsnew/v3.0.0.rst
index 4accf8be46b9e..f316f6b44c1b4 100644
--- a/doc/source/whatsnew/v3.0.0.rst
+++ b/doc/source/whatsnew/v3.0.0.rst
@@ -104,6 +104,8 @@ Removal of prior version deprecations/changes
- Removed :meth:`DataFrameGroupby.fillna` and :meth:`SeriesGroupBy.fillna` (:issue:`55719`)
- 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:
diff --git a/pandas/__init__.py b/pandas/__init__.py
index ed524c2bb3619..535522253c415 100644
--- a/pandas/__init__.py
+++ b/pandas/__init__.py
@@ -1,6 +1,5 @@
from __future__ import annotations
-import os
import warnings
__docformat__ = "restructuredtext"
@@ -193,16 +192,6 @@
__git_version__ = v.get("full-revisionid")
del get_versions, v
-# GH#55043 - deprecation of the data_manager option
-if "PANDAS_DATA_MANAGER" in os.environ:
- warnings.warn(
- "The env variable PANDAS_DATA_MANAGER is set. The data_manager option is "
- "deprecated and will be removed in a future version. Only the BlockManager "
- "will be available. Unset this environment variable to silence this warning.",
- FutureWarning,
- stacklevel=2,
- )
-
# DeprecationWarning for missing pyarrow
from pandas.compat.pyarrow import pa_version_under10p1, pa_not_found
@@ -232,7 +221,7 @@
del VERSIONS, pa_msg
# Delete all unnecessary imported modules
-del pa_version_under10p1, pa_not_found, warnings, os
+del pa_version_under10p1, pa_not_found, warnings
# module level doc-string
__doc__ = """
diff --git a/pandas/_config/__init__.py b/pandas/_config/__init__.py
index 97784c924dab4..5b2bac2e8d747 100644
--- a/pandas/_config/__init__.py
+++ b/pandas/_config/__init__.py
@@ -33,18 +33,12 @@
def using_copy_on_write() -> bool:
_mode_options = _global_config["mode"]
- return (
- _mode_options["copy_on_write"] is True
- and _mode_options["data_manager"] == "block"
- )
+ return _mode_options["copy_on_write"] is True
def warn_copy_on_write() -> bool:
_mode_options = _global_config["mode"]
- return (
- _mode_options["copy_on_write"] == "warn"
- and _mode_options["data_manager"] == "block"
- )
+ return _mode_options["copy_on_write"] == "warn"
def using_nullable_dtypes() -> bool:
diff --git a/pandas/_typing.py b/pandas/_typing.py
index 0233fbbcf3a12..c704516f74300 100644
--- a/pandas/_typing.py
+++ b/pandas/_typing.py
@@ -61,9 +61,7 @@
)
from pandas.core.indexes.base import Index
from pandas.core.internals import (
- ArrayManager,
BlockManager,
- SingleArrayManager,
SingleBlockManager,
)
from pandas.core.resample import Resampler
@@ -382,11 +380,7 @@ def closed(self) -> bool:
]
# internals
-Manager = Union[
- "ArrayManager", "SingleArrayManager", "BlockManager", "SingleBlockManager"
-]
-SingleManager = Union["SingleArrayManager", "SingleBlockManager"]
-Manager2D = Union["ArrayManager", "BlockManager"]
+Manager = Union["BlockManager", "SingleBlockManager"]
# indexing
# PositionalIndexer -> valid 1D positional indexer, e.g. can pass
diff --git a/pandas/conftest.py b/pandas/conftest.py
index 9979488bb6d5d..26e03ca30d4fb 100644
--- a/pandas/conftest.py
+++ b/pandas/conftest.py
@@ -48,8 +48,6 @@
utc,
)
-from pandas._config.config import _get_option
-
import pandas.util._test_decorators as td
from pandas.core.dtypes.dtypes import (
@@ -1965,10 +1963,7 @@ def using_copy_on_write() -> bool:
"""
Fixture to check if Copy-on-Write is enabled.
"""
- return (
- pd.options.mode.copy_on_write is True
- and _get_option("mode.data_manager", silent=True) == "block"
- )
+ return pd.options.mode.copy_on_write is True
@pytest.fixture
@@ -1976,10 +1971,7 @@ def warn_copy_on_write() -> bool:
"""
Fixture to check if Copy-on-Write is in warning mode.
"""
- return (
- pd.options.mode.copy_on_write == "warn"
- and _get_option("mode.data_manager", silent=True) == "block"
- )
+ return pd.options.mode.copy_on_write == "warn"
@pytest.fixture
diff --git a/pandas/core/apply.py b/pandas/core/apply.py
index 5b2293aeebbe7..ebd714f9c14d4 100644
--- a/pandas/core/apply.py
+++ b/pandas/core/apply.py
@@ -1256,7 +1256,7 @@ def series_generator(self) -> Generator[Series, None, None]:
ser = self.obj._ixs(0, axis=0)
mgr = ser._mgr
- is_view = mgr.blocks[0].refs.has_reference() # type: ignore[union-attr]
+ is_view = mgr.blocks[0].refs.has_reference()
if isinstance(ser.dtype, ExtensionDtype):
# values will be incorrect for this block
@@ -1278,7 +1278,7 @@ def series_generator(self) -> Generator[Series, None, None]:
# -> if that happened and `ser` is already a copy, then we reset
# the refs here to avoid triggering a unnecessary CoW inside the
# applied function (https://github.com/pandas-dev/pandas/pull/56212)
- mgr.blocks[0].refs = BlockValuesRefs(mgr.blocks[0]) # type: ignore[union-attr]
+ mgr.blocks[0].refs = BlockValuesRefs(mgr.blocks[0])
yield ser
@staticmethod
diff --git a/pandas/core/arraylike.py b/pandas/core/arraylike.py
index fd585b3e19468..62f6737d86d51 100644
--- a/pandas/core/arraylike.py
+++ b/pandas/core/arraylike.py
@@ -263,10 +263,7 @@ def array_ufunc(self, ufunc: np.ufunc, method: str, *inputs: Any, **kwargs: Any)
Series,
)
from pandas.core.generic import NDFrame
- from pandas.core.internals import (
- ArrayManager,
- BlockManager,
- )
+ from pandas.core.internals import BlockManager
cls = type(self)
@@ -350,7 +347,7 @@ def _reconstruct(result):
if method == "outer":
raise NotImplementedError
return result
- if isinstance(result, (BlockManager, ArrayManager)):
+ if isinstance(result, BlockManager):
# we went through BlockManager.apply e.g. np.sqrt
result = self._constructor_from_mgr(result, axes=result.axes)
else:
diff --git a/pandas/core/config_init.py b/pandas/core/config_init.py
index 0a9d5af7cbd42..f9e6b3296eb13 100644
--- a/pandas/core/config_init.py
+++ b/pandas/core/config_init.py
@@ -436,32 +436,6 @@ def use_inf_as_na_cb(key) -> None:
"version. Convert inf values to NaN before operating instead.",
)
-data_manager_doc = """
-: string
- Internal data manager type; can be "block" or "array". Defaults to "block",
- unless overridden by the 'PANDAS_DATA_MANAGER' environment variable (needs
- to be set before pandas is imported).
-"""
-
-
-with cf.config_prefix("mode"):
- cf.register_option(
- "data_manager",
- # Get the default from an environment variable, if set, otherwise defaults
- # to "block". This environment variable can be set for testing.
- os.environ.get("PANDAS_DATA_MANAGER", "block"),
- data_manager_doc,
- validator=is_one_of_factory(["block", "array"]),
- )
-
-cf.deprecate_option(
- # GH#55043
- "mode.data_manager",
- "data_manager option is deprecated and will be removed in a future "
- "version. Only the BlockManager will be available.",
-)
-
-
# TODO better name?
copy_on_write_doc = """
: bool
diff --git a/pandas/core/frame.py b/pandas/core/frame.py
index 8a0783dab5214..b3e18d6ceaddd 100644
--- a/pandas/core/frame.py
+++ b/pandas/core/frame.py
@@ -44,7 +44,6 @@
using_copy_on_write,
warn_copy_on_write,
)
-from pandas._config.config import _get_option
from pandas._libs import (
algos as libalgos,
@@ -168,15 +167,11 @@
check_bool_indexer,
check_dict_or_set_indexers,
)
-from pandas.core.internals import (
- ArrayManager,
- BlockManager,
-)
+from pandas.core.internals import BlockManager
from pandas.core.internals.construction import (
arrays_to_mgr,
dataclasses_to_dicts,
dict_to_mgr,
- mgr_to_mgr,
ndarray_to_mgr,
nested_data_to_arrays,
rec_array_to_mgr,
@@ -648,7 +643,7 @@ class DataFrame(NDFrame, OpsMixin):
_HANDLED_TYPES = (Series, Index, ExtensionArray, np.ndarray)
_accessors: set[str] = {"sparse"}
_hidden_attrs: frozenset[str] = NDFrame._hidden_attrs | frozenset([])
- _mgr: BlockManager | ArrayManager
+ _mgr: BlockManager
# similar to __array_priority__, positions DataFrame before Series, Index,
# and ExtensionArray. Should NOT be overridden by subclasses.
@@ -702,7 +697,7 @@ def __init__(
# to avoid the result sharing the same Manager
data = data.copy(deep=False)
- if isinstance(data, (BlockManager, ArrayManager)):
+ if isinstance(data, BlockManager):
if not allow_mgr:
# GH#52419
warnings.warn(
@@ -722,8 +717,6 @@ def __init__(
NDFrame.__init__(self, data)
return
- manager = _get_option("mode.data_manager", silent=True)
-
is_pandas_object = isinstance(data, (Series, Index, ExtensionArray))
data_dtype = getattr(data, "dtype", None)
original_dtype = dtype
@@ -738,14 +731,6 @@ def __init__(
if isinstance(data, dict):
# retain pre-GH#38939 default behavior
copy = True
- elif (
- manager == "array"
- and isinstance(data, (np.ndarray, ExtensionArray))
- and data.ndim == 2
- ):
- # INFO(ArrayManager) by default copy the 2D input array to get
- # contiguous 1D arrays
- copy = True
elif using_copy_on_write() and not isinstance(
data, (Index, DataFrame, Series)
):
@@ -759,14 +744,14 @@ def __init__(
dtype = dtype if dtype is not None else pandas_dtype(object)
data = []
- if isinstance(data, (BlockManager, ArrayManager)):
+ if isinstance(data, BlockManager):
mgr = self._init_mgr(
data, axes={"index": index, "columns": columns}, dtype=dtype, copy=copy
)
elif isinstance(data, dict):
# GH#38939 de facto copy defaults to False only in non-dict cases
- mgr = dict_to_mgr(data, index, columns, dtype=dtype, copy=copy, typ=manager)
+ mgr = dict_to_mgr(data, index, columns, dtype=dtype, copy=copy)
elif isinstance(data, ma.MaskedArray):
from numpy.ma import mrecords
@@ -786,7 +771,6 @@ def __init__(
columns,
dtype=dtype,
copy=copy,
- typ=manager,
)
elif isinstance(data, (np.ndarray, Series, Index, ExtensionArray)):
@@ -799,7 +783,6 @@ def __init__(
columns,
dtype,
copy,
- typ=manager,
)
elif getattr(data, "name", None) is not None:
# i.e. Series/Index with non-None name
@@ -811,7 +794,6 @@ def __init__(
index,
columns,
dtype=dtype,
- typ=manager,
copy=_copy,
)
else:
@@ -821,7 +803,6 @@ def __init__(
columns,
dtype=dtype,
copy=copy,
- typ=manager,
)
# For data is list-like, or Iterable (will consume into list)
@@ -852,7 +833,6 @@ def __init__(
columns,
index,
dtype=dtype,
- typ=manager,
)
else:
mgr = ndarray_to_mgr(
@@ -861,7 +841,6 @@ def __init__(
columns,
dtype=dtype,
copy=copy,
- typ=manager,
)
else:
mgr = dict_to_mgr(
@@ -869,7 +848,6 @@ def __init__(
index,
columns if columns is not None else default_index(0),
dtype=dtype,
- typ=manager,
)
# For data is scalar
else:
@@ -890,7 +868,7 @@ def __init__(
construct_1d_arraylike_from_scalar(data, len(index), dtype)
for _ in range(len(columns))
]
- mgr = arrays_to_mgr(values, columns, index, dtype=None, typ=manager)
+ mgr = arrays_to_mgr(values, columns, index, dtype=None)
else:
arr2d = construct_2d_arraylike_from_scalar(
data,
@@ -906,12 +884,8 @@ def __init__(
columns,
dtype=arr2d.dtype,
copy=False,
- typ=manager,
)
- # ensure correct Manager type according to settings
- mgr = mgr_to_mgr(mgr, typ=manager)
-
NDFrame.__init__(self, mgr)
if original_dtype is None and is_pandas_object and data_dtype == np.object_:
@@ -1089,8 +1063,6 @@ def _can_fast_transpose(self) -> bool:
"""
Can we transpose this DataFrame without creating any new array objects.
"""
- if isinstance(self._mgr, ArrayManager):
- return False
blocks = self._mgr.blocks
if len(blocks) != 1:
return False
@@ -1106,13 +1078,6 @@ def _values(self) -> np.ndarray | DatetimeArray | TimedeltaArray | PeriodArray:
"""
mgr = self._mgr
- if isinstance(mgr, ArrayManager):
- if len(mgr.arrays) == 1 and not is_1d_only_ea_dtype(mgr.arrays[0].dtype):
- # error: Item "ExtensionArray" of "Union[ndarray, ExtensionArray]"
- # has no attribute "reshape"
- return mgr.arrays[0].reshape(-1, 1) # type: ignore[union-attr]
- return ensure_wrapped_if_datetimelike(self.values)
-
blocks = mgr.blocks
if len(blocks) != 1:
return ensure_wrapped_if_datetimelike(self.values)
@@ -1541,7 +1506,7 @@ def iterrows(self) -> Iterable[tuple[Hashable, Series]]:
for k, v in zip(self.index, self.values):
s = klass(v, index=columns, name=k).__finalize__(self)
if using_cow and self._mgr.is_single_block:
- s._mgr.add_references(self._mgr) # type: ignore[arg-type]
+ s._mgr.add_references(self._mgr)
yield k, s
def itertuples(
@@ -2505,9 +2470,7 @@ def maybe_reorder(
columns = columns.drop(exclude)
- manager = _get_option("mode.data_manager", silent=True)
- mgr = arrays_to_mgr(arrays, columns, result_index, typ=manager)
-
+ mgr = arrays_to_mgr(arrays, columns, result_index)
return cls._from_mgr(mgr, axes=mgr.axes)
def to_records(
@@ -2706,7 +2669,6 @@ def _from_arrays(
if dtype is not None:
dtype = pandas_dtype(dtype)
- manager = _get_option("mode.data_manager", silent=True)
columns = ensure_index(columns)
if len(columns) != len(arrays):
raise ValueError("len(columns) must match len(arrays)")
@@ -2716,7 +2678,6 @@ def _from_arrays(
index,
dtype=dtype,
verify_integrity=verify_integrity,
- typ=manager,
)
return cls._from_mgr(mgr, axes=mgr.axes)
@@ -3859,7 +3820,7 @@ def transpose(self, *args, copy: bool = False) -> DataFrame:
dtype=new_vals.dtype,
)
if using_copy_on_write() and len(self) > 0:
- result._mgr.add_references(self._mgr) # type: ignore[arg-type]
+ result._mgr.add_references(self._mgr)
elif (
self._is_homogeneous_type
@@ -3999,11 +3960,8 @@ def _iter_column_arrays(self) -> Iterator[ArrayLike]:
Warning! The returned array is a view but doesn't handle Copy-on-Write,
so this should be used with caution (for read-only purposes).
"""
- if isinstance(self._mgr, ArrayManager):
- yield from self._mgr.arrays
- else:
- for i in range(len(self.columns)):
- yield self._get_column_array(i)
+ for i in range(len(self.columns)):
+ yield self._get_column_array(i)
def _getitem_nocopy(self, key: list):
"""
@@ -4246,7 +4204,7 @@ def __setitem__(self, key, value) -> None:
warn_copy_on_write()
or (
not warn_copy_on_write()
- and any(b.refs.has_reference() for b in self._mgr.blocks) # type: ignore[union-attr]
+ and any(b.refs.has_reference() for b in self._mgr.blocks)
)
):
warnings.warn(
@@ -7917,13 +7875,7 @@ def _dispatch_frame_op(
# TODO operate_blockwise expects a manager of the same type
bm = self._mgr.operate_blockwise(
- # error: Argument 1 to "operate_blockwise" of "ArrayManager" has
- # incompatible type "Union[ArrayManager, BlockManager]"; expected
- # "ArrayManager"
- # error: Argument 1 to "operate_blockwise" of "BlockManager" has
- # incompatible type "Union[ArrayManager, BlockManager]"; expected
- # "BlockManager"
- right._mgr, # type: ignore[arg-type]
+ right._mgr,
array_op,
)
return self._constructor_from_mgr(bm, axes=bm.axes)
@@ -11403,9 +11355,7 @@ def func(values: np.ndarray):
def blk_func(values, axis: Axis = 1):
if isinstance(values, ExtensionArray):
- if not is_1d_only_ea_dtype(values.dtype) and not isinstance(
- self._mgr, ArrayManager
- ):
+ if not is_1d_only_ea_dtype(values.dtype):
return values._reduce(name, axis=1, skipna=skipna, **kwds)
has_keepdims = dtype_has_keepdims.get(values.dtype)
if has_keepdims is None:
@@ -12526,8 +12476,6 @@ def _to_dict_of_blocks(self):
Internal ONLY - only works for BlockManager
"""
mgr = self._mgr
- # convert to BlockManager if needed -> this way support ArrayManager as well
- mgr = cast(BlockManager, mgr_to_mgr(mgr, "block"))
return {
k: self._constructor_from_mgr(v, axes=v.axes).__finalize__(self)
for k, v in mgr.to_dict().items()
diff --git a/pandas/core/generic.py b/pandas/core/generic.py
index 9440ad67256e1..037f809eaabca 100644
--- a/pandas/core/generic.py
+++ b/pandas/core/generic.py
@@ -176,15 +176,8 @@
default_index,
ensure_index,
)
-from pandas.core.internals import (
- ArrayManager,
- BlockManager,
- SingleArrayManager,
-)
-from pandas.core.internals.construction import (
- mgr_to_mgr,
- ndarray_to_mgr,
-)
+from pandas.core.internals import BlockManager
+from pandas.core.internals.construction import ndarray_to_mgr
from pandas.core.methods.describe import describe_ndframe
from pandas.core.missing import (
clean_fill_method,
@@ -318,29 +311,6 @@ def _init_mgr(
mgr = mgr.astype(dtype=dtype)
return mgr
- @final
- def _as_manager(self, typ: str, copy: bool_t = True) -> Self:
- """
- Private helper function to create a DataFrame with specific manager.
-
- Parameters
- ----------
- typ : {"block", "array"}
- copy : bool, default True
- Only controls whether the conversion from Block->ArrayManager
- copies the 1D arrays (to ensure proper/contiguous memory layout).
-
- Returns
- -------
- DataFrame
- New DataFrame using specified manager type. Is not guaranteed
- to be a copy or not.
- """
- new_mgr: Manager
- new_mgr = mgr_to_mgr(self._mgr, typ=typ, copy=copy)
- # fastpath of passing a manager doesn't check the option/manager class
- return self._constructor_from_mgr(new_mgr, axes=new_mgr.axes).__finalize__(self)
-
@classmethod
def _from_mgr(cls, mgr: Manager, axes: list[Index]) -> Self:
"""
@@ -679,9 +649,9 @@ def _is_view_after_cow_rules(self):
# Only to be used in cases of chained assignment checks, this is a
# simplified check that assumes that either the whole object is a view
# or a copy
- if len(self._mgr.blocks) == 0: # type: ignore[union-attr]
+ if len(self._mgr.blocks) == 0:
return False
- return self._mgr.blocks[0].refs.has_reference() # type: ignore[union-attr]
+ return self._mgr.blocks[0].refs.has_reference()
@property
def shape(self) -> tuple[int, ...]:
@@ -865,7 +835,6 @@ def swapaxes(self, axis1: Axis, axis2: Axis, copy: bool_t | None = None) -> Self
new_axes[1],
dtype=None,
copy=False,
- typ="block",
)
assert isinstance(new_mgr, BlockManager)
assert isinstance(self._mgr, BlockManager)
@@ -6381,8 +6350,6 @@ def _protect_consolidate(self, f):
Consolidate _mgr -- if the blocks have changed, then clear the
cache
"""
- if isinstance(self._mgr, (ArrayManager, SingleArrayManager)):
- return f()
blocks_before = len(self._mgr.blocks)
result = f()
if len(self._mgr.blocks) != blocks_before:
@@ -7049,7 +7016,7 @@ def convert_dtypes(
dtype: string
"""
check_dtype_backend(dtype_backend)
- new_mgr = self._mgr.convert_dtypes( # type: ignore[union-attr]
+ new_mgr = self._mgr.convert_dtypes(
infer_objects=infer_objects,
convert_string=convert_string,
convert_integer=convert_integer,
@@ -12719,8 +12686,8 @@ def _inplace_method(self, other, op) -> Self:
and not (warn_copy_on_write() and not warn)
):
# GH#36498 this inplace op can _actually_ be inplace.
- # Item "ArrayManager" of "Union[ArrayManager, SingleArrayManager,
- # BlockManager, SingleBlockManager]" has no attribute "setitem_inplace"
+ # Item "BlockManager" of "Union[BlockManager, SingleBlockManager]" has
+ # no attribute "setitem_inplace"
self._mgr.setitem_inplace( # type: ignore[union-attr]
slice(None), result._values, warn=warn
)
diff --git a/pandas/core/groupby/generic.py b/pandas/core/groupby/generic.py
index 351b4bff0162e..1a23d237dca46 100644
--- a/pandas/core/groupby/generic.py
+++ b/pandas/core/groupby/generic.py
@@ -96,11 +96,11 @@
from pandas._typing import (
ArrayLike,
AxisInt,
+ BlockManager,
CorrelationMethod,
IndexLabel,
Manager,
- Manager2D,
- SingleManager,
+ SingleBlockManager,
TakeIndexer,
)
@@ -153,7 +153,7 @@ def _wrap_agged_manager(self, mgr: Manager) -> Series:
def _get_data_to_aggregate(
self, *, numeric_only: bool = False, name: str | None = None
- ) -> SingleManager:
+ ) -> SingleBlockManager:
ser = self._obj_with_exclusions
single = ser._mgr
if numeric_only and not is_numeric_dtype(ser.dtype):
@@ -1553,7 +1553,7 @@ def _cython_transform(
# test_transform_numeric_ret
# With self.axis == 1, _get_data_to_aggregate does a transpose
# so we always have a single block.
- mgr: Manager2D = self._get_data_to_aggregate(
+ mgr: BlockManager = self._get_data_to_aggregate(
numeric_only=numeric_only, name=how
)
@@ -1562,10 +1562,7 @@ def arr_func(bvalues: ArrayLike) -> ArrayLike:
"transform", bvalues, how, 1, **kwargs
)
- # We could use `mgr.apply` here and not have to set_axis, but
- # we would have to do shape gymnastics for ArrayManager compat
- res_mgr = mgr.grouped_reduce(arr_func)
- res_mgr.set_axis(1, mgr.axes[1])
+ res_mgr = mgr.apply(arr_func)
res_df = self.obj._constructor_from_mgr(res_mgr, axes=res_mgr.axes)
res_df = self._maybe_transpose_result(res_df)
@@ -1866,7 +1863,7 @@ def _gotitem(self, key, ndim: int, subset=None):
def _get_data_to_aggregate(
self, *, numeric_only: bool = False, name: str | None = None
- ) -> Manager2D:
+ ) -> BlockManager:
obj = self._obj_with_exclusions
if self.axis == 1:
mgr = obj.T._mgr
@@ -1877,7 +1874,7 @@ def _get_data_to_aggregate(
mgr = mgr.get_numeric_data()
return mgr
- def _wrap_agged_manager(self, mgr: Manager2D) -> DataFrame:
+ def _wrap_agged_manager(self, mgr: BlockManager) -> DataFrame:
return self.obj._constructor_from_mgr(mgr, axes=mgr.axes)
def _apply_to_column_groupbys(self, func) -> DataFrame:
diff --git a/pandas/core/groupby/grouper.py b/pandas/core/groupby/grouper.py
index d36bfa62f4be5..08ee786170674 100644
--- a/pandas/core/groupby/grouper.py
+++ b/pandas/core/groupby/grouper.py
@@ -1007,10 +1007,7 @@ def is_in_obj(gpr) -> bool:
except (KeyError, IndexError, InvalidIndexError, OutOfBoundsDatetime):
return False
if isinstance(gpr, Series) and isinstance(obj_gpr_column, Series):
- return gpr._mgr.references_same_values( # type: ignore[union-attr]
- obj_gpr_column._mgr, # type: ignore[arg-type]
- 0,
- )
+ return gpr._mgr.references_same_values(obj_gpr_column._mgr, 0)
return False
try:
return gpr is obj[gpr.name]
diff --git a/pandas/core/internals/__init__.py b/pandas/core/internals/__init__.py
index 2eb413440ba9c..da394e783be4b 100644
--- a/pandas/core/internals/__init__.py
+++ b/pandas/core/internals/__init__.py
@@ -1,8 +1,4 @@
from pandas.core.internals.api import make_block # 2023-09-18 pyarrow uses this
-from pandas.core.internals.array_manager import (
- ArrayManager,
- SingleArrayManager,
-)
from pandas.core.internals.base import (
DataManager,
SingleDataManager,
@@ -19,11 +15,9 @@
"ExtensionBlock", # pylint: disable=undefined-all-variable
"make_block",
"DataManager",
- "ArrayManager",
"BlockManager",
"SingleDataManager",
"SingleBlockManager",
- "SingleArrayManager",
"concatenate_managers",
]
diff --git a/pandas/core/internals/array_manager.py b/pandas/core/internals/array_manager.py
deleted file mode 100644
index ee62441ab8f55..0000000000000
--- a/pandas/core/internals/array_manager.py
+++ /dev/null
@@ -1,1342 +0,0 @@
-"""
-Experimental manager based on storing a collection of 1D arrays
-"""
-from __future__ import annotations
-
-import itertools
-from typing import (
- TYPE_CHECKING,
- Callable,
- Literal,
-)
-
-import numpy as np
-
-from pandas._libs import (
- NaT,
- lib,
-)
-
-from pandas.core.dtypes.astype import (
- astype_array,
- astype_array_safe,
-)
-from pandas.core.dtypes.cast import (
- ensure_dtype_can_hold_na,
- find_common_type,
- infer_dtype_from_scalar,
- np_find_common_type,
-)
-from pandas.core.dtypes.common import (
- ensure_platform_int,
- is_datetime64_ns_dtype,
- is_integer,
- is_numeric_dtype,
- is_object_dtype,
- is_timedelta64_ns_dtype,
-)
-from pandas.core.dtypes.dtypes import ExtensionDtype
-from pandas.core.dtypes.generic import (
- ABCDataFrame,
- ABCSeries,
-)
-from pandas.core.dtypes.missing import (
- array_equals,
- isna,
- na_value_for_dtype,
-)
-
-import pandas.core.algorithms as algos
-from pandas.core.array_algos.quantile import quantile_compat
-from pandas.core.array_algos.take import take_1d
-from pandas.core.arrays import (
- DatetimeArray,
- ExtensionArray,
- NumpyExtensionArray,
- TimedeltaArray,
-)
-from pandas.core.construction import (
- ensure_wrapped_if_datetimelike,
- extract_array,
- sanitize_array,
-)
-from pandas.core.indexers import (
- maybe_convert_indices,
- validate_indices,
-)
-from pandas.core.indexes.api import (
- Index,
- ensure_index,
-)
-from pandas.core.indexes.base import get_values_for_csv
-from pandas.core.internals.base import (
- DataManager,
- SingleDataManager,
- ensure_np_dtype,
- interleaved_dtype,
-)
-from pandas.core.internals.blocks import (
- BlockPlacement,
- ensure_block_shape,
- external_values,
- extract_pandas_array,
- maybe_coerce_values,
- new_block,
-)
-from pandas.core.internals.managers import make_na_array
-
-if TYPE_CHECKING:
- from collections.abc import Hashable
-
- from pandas._typing import (
- ArrayLike,
- AxisInt,
- DtypeObj,
- QuantileInterpolation,
- Self,
- npt,
- )
-
-
-class BaseArrayManager(DataManager):
- """
- Core internal data structure to implement DataFrame and Series.
-
- Alternative to the BlockManager, storing a list of 1D arrays instead of
- Blocks.
-
- This is *not* a public API class
-
- Parameters
- ----------
- arrays : Sequence of arrays
- axes : Sequence of Index
- verify_integrity : bool, default True
-
- """
-
- __slots__ = [
- "_axes", # private attribute, because 'axes' has different order, see below
- "arrays",
- ]
-
- arrays: list[np.ndarray | ExtensionArray]
- _axes: list[Index]
-
- def __init__(
- self,
- arrays: list[np.ndarray | ExtensionArray],
- axes: list[Index],
- verify_integrity: bool = True,
- ) -> None:
- raise NotImplementedError
-
- def make_empty(self, axes=None) -> Self:
- """Return an empty ArrayManager with the items axis of len 0 (no columns)"""
- if axes is None:
- axes = [self.axes[1:], Index([])]
-
- arrays: list[np.ndarray | ExtensionArray] = []
- return type(self)(arrays, axes)
-
- @property
- def items(self) -> Index:
- return self._axes[-1]
-
- @property
- # error: Signature of "axes" incompatible with supertype "DataManager"
- def axes(self) -> list[Index]: # type: ignore[override]
- # mypy doesn't work to override attribute with property
- # see https://github.com/python/mypy/issues/4125
- """Axes is BlockManager-compatible order (columns, rows)"""
- return [self._axes[1], self._axes[0]]
-
- @property
- def shape_proper(self) -> tuple[int, ...]:
- # this returns (n_rows, n_columns)
- return tuple(len(ax) for ax in self._axes)
-
- @staticmethod
- def _normalize_axis(axis: AxisInt) -> int:
- # switch axis
- axis = 1 if axis == 0 else 0
- return axis
-
- def set_axis(self, axis: AxisInt, new_labels: Index) -> None:
- # Caller is responsible for ensuring we have an Index object.
- self._validate_set_axis(axis, new_labels)
- axis = self._normalize_axis(axis)
- self._axes[axis] = new_labels
-
- def get_dtypes(self) -> npt.NDArray[np.object_]:
- return np.array([arr.dtype for arr in self.arrays], dtype="object")
-
- def add_references(self, mgr: BaseArrayManager) -> None:
- """
- Only implemented on the BlockManager level
- """
- return
-
- def __getstate__(self):
- return self.arrays, self._axes
-
- def __setstate__(self, state) -> None:
- self.arrays = state[0]
- self._axes = state[1]
-
- def __repr__(self) -> str:
- output = type(self).__name__
- output += f"\nIndex: {self._axes[0]}"
- if self.ndim == 2:
- output += f"\nColumns: {self._axes[1]}"
- output += f"\n{len(self.arrays)} arrays:"
- for arr in self.arrays:
- output += f"\n{arr.dtype}"
- return output
-
- def apply(
- self,
- f,
- align_keys: list[str] | None = None,
- **kwargs,
- ) -> Self:
- """
- Iterate over the arrays, collect and create a new ArrayManager.
-
- Parameters
- ----------
- f : str or callable
- Name of the Array method to apply.
- align_keys: List[str] or None, default None
- **kwargs
- Keywords to pass to `f`
-
- Returns
- -------
- ArrayManager
- """
- assert "filter" not in kwargs
-
- align_keys = align_keys or []
- result_arrays: list[ArrayLike] = []
- # fillna: Series/DataFrame is responsible for making sure value is aligned
-
- aligned_args = {k: kwargs[k] for k in align_keys}
-
- if f == "apply":
- f = kwargs.pop("func")
-
- for i, arr in enumerate(self.arrays):
- if aligned_args:
- for k, obj in aligned_args.items():
- if isinstance(obj, (ABCSeries, ABCDataFrame)):
- # The caller is responsible for ensuring that
- # obj.axes[-1].equals(self.items)
- if obj.ndim == 1:
- kwargs[k] = obj.iloc[i]
- else:
- kwargs[k] = obj.iloc[:, i]._values
- else:
- # otherwise we have an array-like
- kwargs[k] = obj[i]
-
- if callable(f):
- applied = f(arr, **kwargs)
- else:
- applied = getattr(arr, f)(**kwargs)
-
- result_arrays.append(applied)
-
- new_axes = self._axes
- return type(self)(result_arrays, new_axes)
-
- def apply_with_block(self, f, align_keys=None, **kwargs) -> Self:
- # switch axis to follow BlockManager logic
- swap_axis = True
- if f == "interpolate":
- swap_axis = False
- if swap_axis and "axis" in kwargs and self.ndim == 2:
- kwargs["axis"] = 1 if kwargs["axis"] == 0 else 0
-
- align_keys = align_keys or []
- aligned_args = {k: kwargs[k] for k in align_keys}
-
- result_arrays = []
-
- for i, arr in enumerate(self.arrays):
- if aligned_args:
- for k, obj in aligned_args.items():
- if isinstance(obj, (ABCSeries, ABCDataFrame)):
- # The caller is responsible for ensuring that
- # obj.axes[-1].equals(self.items)
- if obj.ndim == 1:
- if self.ndim == 2:
- kwargs[k] = obj.iloc[slice(i, i + 1)]._values
- else:
- kwargs[k] = obj.iloc[:]._values
- else:
- kwargs[k] = obj.iloc[:, [i]]._values
- else:
- # otherwise we have an ndarray
- if obj.ndim == 2:
- kwargs[k] = obj[[i]]
-
- if isinstance(arr.dtype, np.dtype) and not isinstance(arr, np.ndarray):
- # i.e. TimedeltaArray, DatetimeArray with tz=None. Need to
- # convert for the Block constructors.
- arr = np.asarray(arr)
-
- arr = maybe_coerce_values(arr)
- if self.ndim == 2:
- arr = ensure_block_shape(arr, 2)
- bp = BlockPlacement(slice(0, 1, 1))
- block = new_block(arr, placement=bp, ndim=2)
- else:
- bp = BlockPlacement(slice(0, len(self), 1))
- block = new_block(arr, placement=bp, ndim=1)
-
- applied = getattr(block, f)(**kwargs)
- if isinstance(applied, list):
- applied = applied[0]
- arr = applied.values
- if self.ndim == 2 and arr.ndim == 2:
- # 2D for np.ndarray or DatetimeArray/TimedeltaArray
- assert len(arr) == 1
- # error: No overload variant of "__getitem__" of "ExtensionArray"
- # matches argument type "Tuple[int, slice]"
- arr = arr[0, :] # type: ignore[call-overload]
- result_arrays.append(arr)
-
- return type(self)(result_arrays, self._axes)
-
- def setitem(self, indexer, value, warn: bool = True) -> Self:
- return self.apply_with_block("setitem", indexer=indexer, value=value)
-
- def diff(self, n: int) -> Self:
- assert self.ndim == 2 # caller ensures
- return self.apply(algos.diff, n=n)
-
- def astype(self, dtype, copy: bool | None = False, errors: str = "raise") -> Self:
- if copy is None:
- copy = True
-
- return self.apply(astype_array_safe, dtype=dtype, copy=copy, errors=errors)
-
- def convert(self, copy: bool | None) -> Self:
- if copy is None:
- copy = True
-
- def _convert(arr):
- if is_object_dtype(arr.dtype):
- # extract NumpyExtensionArray for tests that patch
- # NumpyExtensionArray._typ
- arr = np.asarray(arr)
- result = lib.maybe_convert_objects(
- arr,
- convert_non_numeric=True,
- )
- if result is arr and copy:
- return arr.copy()
- return result
- else:
- return arr.copy() if copy else arr
-
- return self.apply(_convert)
-
- def get_values_for_csv(
- self, *, float_format, date_format, decimal, na_rep: str = "nan", quoting=None
- ) -> Self:
- return self.apply(
- get_values_for_csv,
- na_rep=na_rep,
- quoting=quoting,
- float_format=float_format,
- date_format=date_format,
- decimal=decimal,
- )
-
- @property
- def any_extension_types(self) -> bool:
- """Whether any of the blocks in this manager are extension blocks"""
- return False # any(block.is_extension for block in self.blocks)
-
- @property
- def is_view(self) -> bool:
- """return a boolean if we are a single block and are a view"""
- # TODO what is this used for?
- return False
-
- @property
- def is_single_block(self) -> bool:
- return len(self.arrays) == 1
-
- def _get_data_subset(self, predicate: Callable) -> Self:
- indices = [i for i, arr in enumerate(self.arrays) if predicate(arr)]
- arrays = [self.arrays[i] for i in indices]
- # TODO copy?
- # Note: using Index.take ensures we can retain e.g. DatetimeIndex.freq,
- # see test_describe_datetime_columns
- taker = np.array(indices, dtype="intp")
- new_cols = self._axes[1].take(taker)
- new_axes = [self._axes[0], new_cols]
- return type(self)(arrays, new_axes, verify_integrity=False)
-
- def get_bool_data(self, copy: bool = False) -> Self:
- """
- Select columns that are bool-dtype and object-dtype columns that are all-bool.
-
- Parameters
- ----------
- copy : bool, default False
- Whether to copy the blocks
- """
- return self._get_data_subset(lambda x: x.dtype == np.dtype(bool))
-
- def get_numeric_data(self, copy: bool = False) -> Self:
- """
- Select columns that have a numeric dtype.
-
- Parameters
- ----------
- copy : bool, default False
- Whether to copy the blocks
- """
- return self._get_data_subset(
- lambda arr: is_numeric_dtype(arr.dtype)
- or getattr(arr.dtype, "_is_numeric", False)
- )
-
- def copy(self, deep: bool | Literal["all"] | None = True) -> Self:
- """
- Make deep or shallow copy of ArrayManager
-
- Parameters
- ----------
- deep : bool or string, default True
- If False, return shallow copy (do not copy data)
- If 'all', copy data and a deep copy of the index
-
- Returns
- -------
- BlockManager
- """
- if deep is None:
- # ArrayManager does not yet support CoW, so deep=None always means
- # deep=True for now
- deep = True
-
- # this preserves the notion of view copying of axes
- if deep:
- # hit in e.g. tests.io.json.test_pandas
-
- def copy_func(ax):
- return ax.copy(deep=True) if deep == "all" else ax.view()
-
- new_axes = [copy_func(ax) for ax in self._axes]
- else:
- new_axes = list(self._axes)
-
- if deep:
- new_arrays = [arr.copy() for arr in self.arrays]
- else:
- new_arrays = list(self.arrays)
- return type(self)(new_arrays, new_axes, verify_integrity=False)
-
- def reindex_indexer(
- self,
- new_axis,
- indexer,
- axis: AxisInt,
- fill_value=None,
- allow_dups: bool = False,
- copy: bool | None = True,
- # ignored keywords
- only_slice: bool = False,
- # ArrayManager specific keywords
- use_na_proxy: bool = False,
- ) -> Self:
- axis = self._normalize_axis(axis)
- return self._reindex_indexer(
- new_axis,
- indexer,
- axis,
- fill_value,
- allow_dups,
- copy,
- use_na_proxy,
- )
-
- def _reindex_indexer(
- self,
- new_axis,
- indexer: npt.NDArray[np.intp] | None,
- axis: AxisInt,
- fill_value=None,
- allow_dups: bool = False,
- copy: bool | None = True,
- use_na_proxy: bool = False,
- ) -> Self:
- """
- Parameters
- ----------
- new_axis : Index
- indexer : ndarray[intp] or None
- axis : int
- fill_value : object, default None
- allow_dups : bool, default False
- copy : bool, default True
-
-
- pandas-indexer with -1's only.
- """
- if copy is None:
- # ArrayManager does not yet support CoW, so deep=None always means
- # deep=True for now
- copy = True
-
- if indexer is None:
- if new_axis is self._axes[axis] and not copy:
- return self
-
- result = self.copy(deep=copy)
- result._axes = list(self._axes)
- result._axes[axis] = new_axis
- return result
-
- # some axes don't allow reindexing with dups
- if not allow_dups:
- self._axes[axis]._validate_can_reindex(indexer)
-
- if axis >= self.ndim:
- raise IndexError("Requested axis not found in manager")
-
- if axis == 1:
- new_arrays = []
- for i in indexer:
- if i == -1:
- arr = self._make_na_array(
- fill_value=fill_value, use_na_proxy=use_na_proxy
- )
- else:
- arr = self.arrays[i]
- if copy:
- arr = arr.copy()
- new_arrays.append(arr)
-
- else:
- validate_indices(indexer, len(self._axes[0]))
- indexer = ensure_platform_int(indexer)
- mask = indexer == -1
- needs_masking = mask.any()
- new_arrays = [
- take_1d(
- arr,
- indexer,
- allow_fill=needs_masking,
- fill_value=fill_value,
- mask=mask,
- # if fill_value is not None else blk.fill_value
- )
- for arr in self.arrays
- ]
-
- new_axes = list(self._axes)
- new_axes[axis] = new_axis
-
- return type(self)(new_arrays, new_axes, verify_integrity=False)
-
- def take(
- self,
- indexer: npt.NDArray[np.intp],
- axis: AxisInt = 1,
- verify: bool = True,
- ) -> Self:
- """
- Take items along any axis.
- """
- assert isinstance(indexer, np.ndarray), type(indexer)
- assert indexer.dtype == np.intp, indexer.dtype
-
- axis = self._normalize_axis(axis)
-
- if not indexer.ndim == 1:
- raise ValueError("indexer should be 1-dimensional")
-
- n = self.shape_proper[axis]
- indexer = maybe_convert_indices(indexer, n, verify=verify)
-
- new_labels = self._axes[axis].take(indexer)
- return self._reindex_indexer(
- new_axis=new_labels, indexer=indexer, axis=axis, allow_dups=True
- )
-
- def _make_na_array(self, fill_value=None, use_na_proxy: bool = False):
- if use_na_proxy:
- assert fill_value is None
- return NullArrayProxy(self.shape_proper[0])
-
- if fill_value is None:
- fill_value = np.nan
-
- dtype, fill_value = infer_dtype_from_scalar(fill_value)
- array_values = make_na_array(dtype, self.shape_proper[:1], fill_value)
- return array_values
-
- def _equal_values(self, other) -> bool:
- """
- Used in .equals defined in base class. Only check the column values
- assuming shape and indexes have already been checked.
- """
- for left, right in zip(self.arrays, other.arrays):
- if not array_equals(left, right):
- return False
- return True
-
- # TODO
- # to_dict
-
-
-class ArrayManager(BaseArrayManager):
- @property
- def ndim(self) -> Literal[2]:
- return 2
-
- def __init__(
- self,
- arrays: list[np.ndarray | ExtensionArray],
- axes: list[Index],
- verify_integrity: bool = True,
- ) -> None:
- # Note: we are storing the axes in "_axes" in the (row, columns) order
- # which contrasts the order how it is stored in BlockManager
- self._axes = axes
- self.arrays = arrays
-
- if verify_integrity:
- self._axes = [ensure_index(ax) for ax in axes]
- arrays = [extract_pandas_array(x, None, 1)[0] for x in arrays]
- self.arrays = [maybe_coerce_values(arr) for arr in arrays]
- self._verify_integrity()
-
- def _verify_integrity(self) -> None:
- n_rows, n_columns = self.shape_proper
- if not len(self.arrays) == n_columns:
- raise ValueError(
- "Number of passed arrays must equal the size of the column Index: "
- f"{len(self.arrays)} arrays vs {n_columns} columns."
- )
- for arr in self.arrays:
- if not len(arr) == n_rows:
- raise ValueError(
- "Passed arrays should have the same length as the rows Index: "
- f"{len(arr)} vs {n_rows} rows"
- )
- if not isinstance(arr, (np.ndarray, ExtensionArray)):
- raise ValueError(
- "Passed arrays should be np.ndarray or ExtensionArray instances, "
- f"got {type(arr)} instead"
- )
- if not arr.ndim == 1:
- raise ValueError(
- "Passed arrays should be 1-dimensional, got array with "
- f"{arr.ndim} dimensions instead."
- )
-
- # --------------------------------------------------------------------
- # Indexing
-
- def fast_xs(self, loc: int) -> SingleArrayManager:
- """
- Return the array corresponding to `frame.iloc[loc]`.
-
- Parameters
- ----------
- loc : int
-
- Returns
- -------
- np.ndarray or ExtensionArray
- """
- dtype = interleaved_dtype([arr.dtype for arr in self.arrays])
-
- values = [arr[loc] for arr in self.arrays]
- if isinstance(dtype, ExtensionDtype):
- result: np.ndarray | ExtensionArray = (
- dtype.construct_array_type()._from_sequence(values, dtype=dtype)
- )
- # for datetime64/timedelta64, the np.ndarray constructor cannot handle pd.NaT
- elif is_datetime64_ns_dtype(dtype):
- result = DatetimeArray._from_sequence(values, dtype=dtype)._ndarray
- elif is_timedelta64_ns_dtype(dtype):
- result = TimedeltaArray._from_sequence(values, dtype=dtype)._ndarray
- else:
- result = np.array(values, dtype=dtype)
- return SingleArrayManager([result], [self._axes[1]])
-
- def get_slice(self, slobj: slice, axis: AxisInt = 0) -> ArrayManager:
- axis = self._normalize_axis(axis)
-
- if axis == 0:
- arrays = [arr[slobj] for arr in self.arrays]
- elif axis == 1:
- arrays = self.arrays[slobj]
-
- new_axes = list(self._axes)
- new_axes[axis] = new_axes[axis]._getitem_slice(slobj)
-
- return type(self)(arrays, new_axes, verify_integrity=False)
-
- def iget(self, i: int) -> SingleArrayManager:
- """
- Return the data as a SingleArrayManager.
- """
- values = self.arrays[i]
- return SingleArrayManager([values], [self._axes[0]])
-
- def iget_values(self, i: int) -> ArrayLike:
- """
- Return the data for column i as the values (ndarray or ExtensionArray).
- """
- return self.arrays[i]
-
- @property
- def column_arrays(self) -> list[ArrayLike]:
- """
- Used in the JSON C code to access column arrays.
- """
-
- return [np.asarray(arr) for arr in self.arrays]
-
- def iset(
- self,
- loc: int | slice | np.ndarray,
- value: ArrayLike,
- inplace: bool = False,
- refs=None,
- ) -> None:
- """
- Set new column(s).
-
- This changes the ArrayManager in-place, but replaces (an) existing
- column(s), not changing column values in-place).
-
- Parameters
- ----------
- loc : integer, slice or boolean mask
- Positional location (already bounds checked)
- value : np.ndarray or ExtensionArray
- inplace : bool, default False
- Whether overwrite existing array as opposed to replacing it.
- """
- # single column -> single integer index
- if lib.is_integer(loc):
- # TODO can we avoid needing to unpack this here? That means converting
- # DataFrame into 1D array when loc is an integer
- if isinstance(value, np.ndarray) and value.ndim == 2:
- assert value.shape[1] == 1
- value = value[:, 0]
-
- # TODO we receive a datetime/timedelta64 ndarray from DataFrame._iset_item
- # but we should avoid that and pass directly the proper array
- value = maybe_coerce_values(value)
-
- assert isinstance(value, (np.ndarray, ExtensionArray))
- assert value.ndim == 1
- assert len(value) == len(self._axes[0])
- self.arrays[loc] = value
- return
-
- # multiple columns -> convert slice or array to integer indices
- elif isinstance(loc, slice):
- indices: range | np.ndarray = range(
- loc.start if loc.start is not None else 0,
- loc.stop if loc.stop is not None else self.shape_proper[1],
- loc.step if loc.step is not None else 1,
- )
- else:
- assert isinstance(loc, np.ndarray)
- assert loc.dtype == "bool"
- indices = np.nonzero(loc)[0]
-
- assert value.ndim == 2
- assert value.shape[0] == len(self._axes[0])
-
- for value_idx, mgr_idx in enumerate(indices):
- # error: No overload variant of "__getitem__" of "ExtensionArray" matches
- # argument type "Tuple[slice, int]"
- value_arr = value[:, value_idx] # type: ignore[call-overload]
- self.arrays[mgr_idx] = value_arr
- return
-
- def column_setitem(
- self, loc: int, idx: int | slice | np.ndarray, value, inplace_only: bool = False
- ) -> None:
- """
- Set values ("setitem") into a single column (not setting the full column).
-
- This is a method on the ArrayManager level, to avoid creating an
- intermediate Series at the DataFrame level (`s = df[loc]; s[idx] = value`)
- """
- if not is_integer(loc):
- raise TypeError("The column index should be an integer")
- arr = self.arrays[loc]
- mgr = SingleArrayManager([arr], [self._axes[0]])
- if inplace_only:
- mgr.setitem_inplace(idx, value)
- else:
- new_mgr = mgr.setitem((idx,), value)
- # update existing ArrayManager in-place
- self.arrays[loc] = new_mgr.arrays[0]
-
- def insert(self, loc: int, item: Hashable, value: ArrayLike, refs=None) -> None:
- """
- Insert item at selected position.
-
- Parameters
- ----------
- loc : int
- item : hashable
- value : np.ndarray or ExtensionArray
- """
- # insert to the axis; this could possibly raise a TypeError
- new_axis = self.items.insert(loc, item)
-
- value = extract_array(value, extract_numpy=True)
- if value.ndim == 2:
- if value.shape[0] == 1:
- # error: No overload variant of "__getitem__" of "ExtensionArray"
- # matches argument type "Tuple[int, slice]"
- value = value[0, :] # type: ignore[call-overload]
- else:
- raise ValueError(
- f"Expected a 1D array, got an array with shape {value.shape}"
- )
- value = maybe_coerce_values(value)
-
- # TODO self.arrays can be empty
- # assert len(value) == len(self.arrays[0])
-
- # TODO is this copy needed?
- arrays = self.arrays.copy()
- arrays.insert(loc, value)
-
- self.arrays = arrays
- self._axes[1] = new_axis
-
- def idelete(self, indexer) -> ArrayManager:
- """
- Delete selected locations in-place (new block and array, same BlockManager)
- """
- to_keep = np.ones(self.shape[0], dtype=np.bool_)
- to_keep[indexer] = False
-
- self.arrays = [self.arrays[i] for i in np.nonzero(to_keep)[0]]
- self._axes = [self._axes[0], self._axes[1][to_keep]]
- return self
-
- # --------------------------------------------------------------------
- # Array-wise Operation
-
- def grouped_reduce(self, func: Callable) -> Self:
- """
- Apply grouped reduction function columnwise, returning a new ArrayManager.
-
- Parameters
- ----------
- func : grouped reduction function
-
- Returns
- -------
- ArrayManager
- """
- result_arrays: list[np.ndarray] = []
- result_indices: list[int] = []
-
- for i, arr in enumerate(self.arrays):
- # grouped_reduce functions all expect 2D arrays
- arr = ensure_block_shape(arr, ndim=2)
- res = func(arr)
- if res.ndim == 2:
- # reverse of ensure_block_shape
- assert res.shape[0] == 1
- res = res[0]
-
- result_arrays.append(res)
- result_indices.append(i)
-
- if len(result_arrays) == 0:
- nrows = 0
- else:
- nrows = result_arrays[0].shape[0]
- index = Index(range(nrows))
-
- columns = self.items
-
- # error: Argument 1 to "ArrayManager" has incompatible type "List[ndarray]";
- # expected "List[Union[ndarray, ExtensionArray]]"
- return type(self)(result_arrays, [index, columns]) # type: ignore[arg-type]
-
- def reduce(self, func: Callable) -> Self:
- """
- Apply reduction function column-wise, returning a single-row ArrayManager.
-
- Parameters
- ----------
- func : reduction function
-
- Returns
- -------
- ArrayManager
- """
- result_arrays: list[np.ndarray] = []
- for i, arr in enumerate(self.arrays):
- res = func(arr, axis=0)
-
- # TODO NaT doesn't preserve dtype, so we need to ensure to create
- # a timedelta result array if original was timedelta
- # what if datetime results in timedelta? (eg std)
- dtype = arr.dtype if res is NaT else None
- result_arrays.append(
- sanitize_array([res], None, dtype=dtype) # type: ignore[arg-type]
- )
-
- index = Index._simple_new(np.array([None], dtype=object)) # placeholder
- columns = self.items
-
- # error: Argument 1 to "ArrayManager" has incompatible type "List[ndarray]";
- # expected "List[Union[ndarray, ExtensionArray]]"
- new_mgr = type(self)(result_arrays, [index, columns]) # type: ignore[arg-type]
- return new_mgr
-
- def operate_blockwise(self, other: ArrayManager, array_op) -> ArrayManager:
- """
- Apply array_op blockwise with another (aligned) BlockManager.
- """
- # TODO what if `other` is BlockManager ?
- left_arrays = self.arrays
- right_arrays = other.arrays
- result_arrays = [
- array_op(left, right) for left, right in zip(left_arrays, right_arrays)
- ]
- return type(self)(result_arrays, self._axes)
-
- def quantile(
- self,
- *,
- qs: Index, # with dtype float64
- transposed: bool = False,
- interpolation: QuantileInterpolation = "linear",
- ) -> ArrayManager:
- arrs = [ensure_block_shape(x, 2) for x in self.arrays]
- new_arrs = [
- quantile_compat(x, np.asarray(qs._values), interpolation) for x in arrs
- ]
- for i, arr in enumerate(new_arrs):
- if arr.ndim == 2:
- assert arr.shape[0] == 1, arr.shape
- new_arrs[i] = arr[0]
-
- axes = [qs, self._axes[1]]
- return type(self)(new_arrs, axes)
-
- # ----------------------------------------------------------------
-
- def unstack(self, unstacker, fill_value) -> ArrayManager:
- """
- Return a BlockManager with all blocks unstacked.
-
- Parameters
- ----------
- unstacker : reshape._Unstacker
- fill_value : Any
- fill_value for newly introduced missing values.
-
- Returns
- -------
- unstacked : BlockManager
- """
- indexer, _ = unstacker._indexer_and_to_sort
- if unstacker.mask.all():
- new_indexer = indexer
- allow_fill = False
- new_mask2D = None
- needs_masking = None
- else:
- new_indexer = np.full(unstacker.mask.shape, -1)
- new_indexer[unstacker.mask] = indexer
- allow_fill = True
- # calculating the full mask once and passing it to take_1d is faster
- # than letting take_1d calculate it in each repeated call
- new_mask2D = (~unstacker.mask).reshape(*unstacker.full_shape)
- needs_masking = new_mask2D.any(axis=0)
- new_indexer2D = new_indexer.reshape(*unstacker.full_shape)
- new_indexer2D = ensure_platform_int(new_indexer2D)
-
- new_arrays = []
- for arr in self.arrays:
- for i in range(unstacker.full_shape[1]):
- if allow_fill:
- # error: Value of type "Optional[Any]" is not indexable [index]
- new_arr = take_1d(
- arr,
- new_indexer2D[:, i],
- allow_fill=needs_masking[i], # type: ignore[index]
- fill_value=fill_value,
- mask=new_mask2D[:, i], # type: ignore[index]
- )
- else:
- new_arr = take_1d(arr, new_indexer2D[:, i], allow_fill=False)
- new_arrays.append(new_arr)
-
- new_index = unstacker.new_index
- new_columns = unstacker.get_new_columns(self._axes[1])
- new_axes = [new_index, new_columns]
-
- return type(self)(new_arrays, new_axes, verify_integrity=False)
-
- def as_array(
- self,
- dtype=None,
- copy: bool = False,
- na_value: object = lib.no_default,
- ) -> np.ndarray:
- """
- Convert the blockmanager data into an numpy array.
-
- Parameters
- ----------
- dtype : object, default None
- Data type of the return array.
- copy : bool, default False
- If True then guarantee that a copy is returned. A value of
- False does not guarantee that the underlying data is not
- copied.
- na_value : object, default lib.no_default
- Value to be used as the missing value sentinel.
-
- Returns
- -------
- arr : ndarray
- """
- if len(self.arrays) == 0:
- empty_arr = np.empty(self.shape, dtype=float)
- return empty_arr.transpose()
-
- # We want to copy when na_value is provided to avoid
- # mutating the original object
- copy = copy or na_value is not lib.no_default
-
- if not dtype:
- dtype = interleaved_dtype([arr.dtype for arr in self.arrays])
-
- dtype = ensure_np_dtype(dtype)
-
- result = np.empty(self.shape_proper, dtype=dtype)
-
- for i, arr in enumerate(self.arrays):
- arr = arr.astype(dtype, copy=copy)
- result[:, i] = arr
-
- if na_value is not lib.no_default:
- result[isna(result)] = na_value
-
- return result
-
- @classmethod
- def concat_horizontal(cls, mgrs: list[Self], axes: list[Index]) -> Self:
- """
- Concatenate uniformly-indexed ArrayManagers horizontally.
- """
- # concatting along the columns -> combine reindexed arrays in a single manager
- arrays = list(itertools.chain.from_iterable([mgr.arrays for mgr in mgrs]))
- new_mgr = cls(arrays, [axes[1], axes[0]], verify_integrity=False)
- return new_mgr
-
- @classmethod
- def concat_vertical(cls, mgrs: list[Self], axes: list[Index]) -> Self:
- """
- Concatenate uniformly-indexed ArrayManagers vertically.
- """
- # concatting along the rows -> concat the reindexed arrays
- # TODO(ArrayManager) doesn't yet preserve the correct dtype
- arrays = [
- concat_arrays([mgrs[i].arrays[j] for i in range(len(mgrs))])
- for j in range(len(mgrs[0].arrays))
- ]
- new_mgr = cls(arrays, [axes[1], axes[0]], verify_integrity=False)
- return new_mgr
-
-
-class SingleArrayManager(BaseArrayManager, SingleDataManager):
- __slots__ = [
- "_axes", # private attribute, because 'axes' has different order, see below
- "arrays",
- ]
-
- arrays: list[np.ndarray | ExtensionArray]
- _axes: list[Index]
-
- @property
- def ndim(self) -> Literal[1]:
- return 1
-
- def __init__(
- self,
- arrays: list[np.ndarray | ExtensionArray],
- axes: list[Index],
- verify_integrity: bool = True,
- ) -> None:
- self._axes = axes
- self.arrays = arrays
-
- if verify_integrity:
- assert len(axes) == 1
- assert len(arrays) == 1
- self._axes = [ensure_index(ax) for ax in self._axes]
- arr = arrays[0]
- arr = maybe_coerce_values(arr)
- arr = extract_pandas_array(arr, None, 1)[0]
- self.arrays = [arr]
- self._verify_integrity()
-
- def _verify_integrity(self) -> None:
- (n_rows,) = self.shape
- assert len(self.arrays) == 1
- arr = self.arrays[0]
- assert len(arr) == n_rows
- if not arr.ndim == 1:
- raise ValueError(
- "Passed array should be 1-dimensional, got array with "
- f"{arr.ndim} dimensions instead."
- )
-
- @staticmethod
- def _normalize_axis(axis):
- return axis
-
- def make_empty(self, axes=None) -> Self:
- """Return an empty ArrayManager with index/array of length 0"""
- if axes is None:
- axes = [Index([], dtype=object)]
- array: np.ndarray = np.array([], dtype=self.dtype)
- return type(self)([array], axes)
-
- @classmethod
- def from_array(cls, array, index) -> SingleArrayManager:
- return cls([array], [index])
-
- # error: Cannot override writeable attribute with read-only property
- @property
- def axes(self) -> list[Index]: # type: ignore[override]
- return self._axes
-
- @property
- def index(self) -> Index:
- return self._axes[0]
-
- @property
- def dtype(self):
- return self.array.dtype
-
- def external_values(self):
- """The array that Series.values returns"""
- return external_values(self.array)
-
- def internal_values(self):
- """The array that Series._values returns"""
- return self.array
-
- def array_values(self):
- """The array that Series.array returns"""
- arr = self.array
- if isinstance(arr, np.ndarray):
- arr = NumpyExtensionArray(arr)
- return arr
-
- @property
- def _can_hold_na(self) -> bool:
- if isinstance(self.array, np.ndarray):
- return self.array.dtype.kind not in "iub"
- else:
- # ExtensionArray
- return self.array._can_hold_na
-
- @property
- def is_single_block(self) -> bool:
- return True
-
- def fast_xs(self, loc: int) -> SingleArrayManager:
- raise NotImplementedError("Use series._values[loc] instead")
-
- def get_slice(self, slobj: slice, axis: AxisInt = 0) -> SingleArrayManager:
- if axis >= self.ndim:
- raise IndexError("Requested axis not found in manager")
-
- new_array = self.array[slobj]
- new_index = self.index._getitem_slice(slobj)
- return type(self)([new_array], [new_index], verify_integrity=False)
-
- def get_rows_with_mask(self, indexer: npt.NDArray[np.bool_]) -> SingleArrayManager:
- new_array = self.array[indexer]
- new_index = self.index[indexer]
- return type(self)([new_array], [new_index])
-
- # error: Signature of "apply" incompatible with supertype "BaseArrayManager"
- def apply(self, func, **kwargs) -> Self: # type: ignore[override]
- if callable(func):
- new_array = func(self.array, **kwargs)
- else:
- new_array = getattr(self.array, func)(**kwargs)
- return type(self)([new_array], self._axes)
-
- def setitem(self, indexer, value, warn: bool = True) -> SingleArrayManager:
- """
- Set values with indexer.
-
- For SingleArrayManager, this backs s[indexer] = value
-
- See `setitem_inplace` for a version that works inplace and doesn't
- return a new Manager.
- """
- if isinstance(indexer, np.ndarray) and indexer.ndim > self.ndim:
- raise ValueError(f"Cannot set values with ndim > {self.ndim}")
- return self.apply_with_block("setitem", indexer=indexer, value=value)
-
- def idelete(self, indexer) -> SingleArrayManager:
- """
- Delete selected locations in-place (new array, same ArrayManager)
- """
- to_keep = np.ones(self.shape[0], dtype=np.bool_)
- to_keep[indexer] = False
-
- self.arrays = [self.arrays[0][to_keep]]
- self._axes = [self._axes[0][to_keep]]
- return self
-
- def _get_data_subset(self, predicate: Callable) -> SingleArrayManager:
- # used in get_numeric_data / get_bool_data
- if predicate(self.array):
- return type(self)(self.arrays, self._axes, verify_integrity=False)
- else:
- return self.make_empty()
-
- def set_values(self, values: ArrayLike) -> None:
- """
- Set (replace) the values of the SingleArrayManager in place.
-
- Use at your own risk! This does not check if the passed values are
- valid for the current SingleArrayManager (length, dtype, etc).
- """
- self.arrays[0] = values
-
- def to_2d_mgr(self, columns: Index) -> ArrayManager:
- """
- Manager analogue of Series.to_frame
- """
- arrays = [self.arrays[0]]
- axes = [self.axes[0], columns]
-
- return ArrayManager(arrays, axes, verify_integrity=False)
-
-
-class NullArrayProxy:
- """
- Proxy object for an all-NA array.
-
- Only stores the length of the array, and not the dtype. The dtype
- will only be known when actually concatenating (after determining the
- common dtype, for which this proxy is ignored).
- Using this object avoids that the internals/concat.py needs to determine
- the proper dtype and array type.
- """
-
- ndim = 1
-
- def __init__(self, n: int) -> None:
- self.n = n
-
- @property
- def shape(self) -> tuple[int]:
- return (self.n,)
-
- def to_array(self, dtype: DtypeObj) -> ArrayLike:
- """
- Helper function to create the actual all-NA array from the NullArrayProxy
- object.
-
- Parameters
- ----------
- arr : NullArrayProxy
- dtype : the dtype for the resulting array
-
- Returns
- -------
- np.ndarray or ExtensionArray
- """
- if isinstance(dtype, ExtensionDtype):
- empty = dtype.construct_array_type()._from_sequence([], dtype=dtype)
- indexer = -np.ones(self.n, dtype=np.intp)
- return empty.take(indexer, allow_fill=True)
- else:
- # when introducing missing values, int becomes float, bool becomes object
- dtype = ensure_dtype_can_hold_na(dtype)
- fill_value = na_value_for_dtype(dtype)
- arr = np.empty(self.n, dtype=dtype)
- arr.fill(fill_value)
- return ensure_wrapped_if_datetimelike(arr)
-
-
-def concat_arrays(to_concat: list) -> ArrayLike:
- """
- Alternative for concat_compat but specialized for use in the ArrayManager.
-
- Differences: only deals with 1D arrays (no axis keyword), assumes
- ensure_wrapped_if_datetimelike and does not skip empty arrays to determine
- the dtype.
- In addition ensures that all NullArrayProxies get replaced with actual
- arrays.
-
- Parameters
- ----------
- to_concat : list of arrays
-
- Returns
- -------
- np.ndarray or ExtensionArray
- """
- # ignore the all-NA proxies to determine the resulting dtype
- to_concat_no_proxy = [x for x in to_concat if not isinstance(x, NullArrayProxy)]
-
- dtypes = {x.dtype for x in to_concat_no_proxy}
- single_dtype = len(dtypes) == 1
-
- if single_dtype:
- target_dtype = to_concat_no_proxy[0].dtype
- elif all(lib.is_np_dtype(x, "iub") for x in dtypes):
- # GH#42092
- target_dtype = np_find_common_type(*dtypes)
- else:
- target_dtype = find_common_type([arr.dtype for arr in to_concat_no_proxy])
-
- to_concat = [
- arr.to_array(target_dtype)
- if isinstance(arr, NullArrayProxy)
- else astype_array(arr, target_dtype, copy=False)
- for arr in to_concat
- ]
-
- if isinstance(to_concat[0], ExtensionArray):
- cls = type(to_concat[0])
- return cls._concat_same_type(to_concat)
-
- result = np.concatenate(to_concat)
-
- # TODO decide on exact behaviour (we shouldn't do this only for empty result)
- # see https://github.com/pandas-dev/pandas/issues/39817
- if len(result) == 0:
- # all empties -> check for bool to not coerce to float
- kinds = {obj.dtype.kind for obj in to_concat_no_proxy}
- if len(kinds) != 1:
- if "b" in kinds:
- result = result.astype(object)
- return result
diff --git a/pandas/core/internals/base.py b/pandas/core/internals/base.py
index 8f16a6623c8cb..d6d588d5e2492 100644
--- a/pandas/core/internals/base.py
+++ b/pandas/core/internals/base.py
@@ -1,6 +1,5 @@
"""
-Base class for the internal managers. Both BlockManager and ArrayManager
-inherit from this class.
+Base class for the internal managers. BlockManager inherits from this class.
"""
from __future__ import annotations
@@ -183,7 +182,7 @@ def fillna(self, value, limit: int | None, inplace: bool, downcast) -> Self:
# Do this validation even if we go through one of the no-op paths
limit = libalgos.validate_limit(None, limit=limit)
- return self.apply_with_block(
+ return self.apply(
"fillna",
value=value,
limit=limit,
@@ -201,7 +200,7 @@ def where(self, other, cond, align: bool) -> Self:
align_keys = ["cond"]
other = extract_array(other, extract_numpy=True)
- return self.apply_with_block(
+ return self.apply(
"where",
align_keys=align_keys,
other=other,
@@ -223,7 +222,7 @@ def putmask(self, mask, new, align: bool = True, warn: bool = True) -> Self:
if not warn:
already_warned.warned_already = True
- return self.apply_with_block(
+ return self.apply(
"putmask",
align_keys=align_keys,
mask=mask,
@@ -234,7 +233,7 @@ def putmask(self, mask, new, align: bool = True, warn: bool = True) -> Self:
@final
def round(self, decimals: int, using_cow: bool = False) -> Self:
- return self.apply_with_block(
+ return self.apply(
"round",
decimals=decimals,
using_cow=using_cow,
@@ -246,7 +245,7 @@ def replace(self, to_replace, value, inplace: bool) -> Self:
# NDFrame.replace ensures the not-is_list_likes here
assert not lib.is_list_like(to_replace)
assert not lib.is_list_like(value)
- return self.apply_with_block(
+ return self.apply(
"replace",
to_replace=to_replace,
value=value,
@@ -257,7 +256,7 @@ def replace(self, to_replace, value, inplace: bool) -> Self:
@final
def replace_regex(self, **kwargs) -> Self:
- return self.apply_with_block(
+ return self.apply(
"_replace_regex",
**kwargs,
using_cow=using_copy_on_write(),
@@ -275,7 +274,7 @@ def replace_list(
"""do a list replace"""
inplace = validate_bool_kwarg(inplace, "inplace")
- bm = self.apply_with_block(
+ bm = self.apply(
"replace_list",
src_list=src_list,
dest_list=dest_list,
@@ -288,7 +287,7 @@ def replace_list(
return bm
def interpolate(self, inplace: bool, **kwargs) -> Self:
- return self.apply_with_block(
+ return self.apply(
"interpolate",
inplace=inplace,
**kwargs,
@@ -297,7 +296,7 @@ def interpolate(self, inplace: bool, **kwargs) -> Self:
)
def pad_or_backfill(self, inplace: bool, **kwargs) -> Self:
- return self.apply_with_block(
+ return self.apply(
"pad_or_backfill",
inplace=inplace,
**kwargs,
@@ -309,7 +308,7 @@ def shift(self, periods: int, fill_value) -> Self:
if fill_value is lib.no_default:
fill_value = None
- return self.apply_with_block("shift", periods=periods, fill_value=fill_value)
+ return self.apply("shift", periods=periods, fill_value=fill_value)
# --------------------------------------------------------------------
# Consolidation: No-ops for all but BlockManager
@@ -333,7 +332,7 @@ def ndim(self) -> Literal[1]:
@property
def array(self) -> ArrayLike:
"""
- Quick access to the backing array of the Block or SingleArrayManager.
+ Quick access to the backing array of the Block.
"""
# error: "SingleDataManager" has no attribute "arrays"; maybe "array"
return self.arrays[0] # type: ignore[attr-defined]
@@ -342,7 +341,7 @@ def setitem_inplace(self, indexer, value, warn: bool = True) -> None:
"""
Set values with indexer.
- For Single[Block/Array]Manager, this backs s[indexer] = value
+ For SingleBlockManager, this backs s[indexer] = value
This is an inplace version of `setitem()`, mutating the manager/values
in place, not returning a new Manager (and Block), and thus never changing
diff --git a/pandas/core/internals/concat.py b/pandas/core/internals/concat.py
index 010ac34b2966a..af16a4175319d 100644
--- a/pandas/core/internals/concat.py
+++ b/pandas/core/internals/concat.py
@@ -39,7 +39,6 @@
)
from pandas.core.construction import ensure_wrapped_if_datetimelike
-from pandas.core.internals.array_manager import ArrayManager
from pandas.core.internals.blocks import (
ensure_block_shape,
new_block_2d,
@@ -56,7 +55,6 @@
ArrayLike,
AxisInt,
DtypeObj,
- Manager2D,
Shape,
)
@@ -67,33 +65,9 @@
)
-def _concatenate_array_managers(
- mgrs: list[ArrayManager], axes: list[Index], concat_axis: AxisInt
-) -> Manager2D:
- """
- Concatenate array managers into one.
-
- Parameters
- ----------
- mgrs_indexers : list of (ArrayManager, {axis: indexer,...}) tuples
- axes : list of Index
- concat_axis : int
-
- Returns
- -------
- ArrayManager
- """
- if concat_axis == 1:
- return mgrs[0].concat_vertical(mgrs, axes)
- else:
- # concatting along the columns -> combine reindexed arrays in a single manager
- assert concat_axis == 0
- return mgrs[0].concat_horizontal(mgrs, axes)
-
-
def concatenate_managers(
mgrs_indexers, axes: list[Index], concat_axis: AxisInt, copy: bool
-) -> Manager2D:
+) -> BlockManager:
"""
Concatenate block managers into one.
@@ -111,18 +85,6 @@ def concatenate_managers(
needs_copy = copy and concat_axis == 0
- # TODO(ArrayManager) this assumes that all managers are of the same type
- if isinstance(mgrs_indexers[0][0], ArrayManager):
- mgrs = _maybe_reindex_columns_na_proxy(axes, mgrs_indexers, needs_copy)
- # error: Argument 1 to "_concatenate_array_managers" has incompatible
- # type "List[BlockManager]"; expected "List[Union[ArrayManager,
- # SingleArrayManager, BlockManager, SingleBlockManager]]"
- return _concatenate_array_managers(
- mgrs, # type: ignore[arg-type]
- axes,
- concat_axis,
- )
-
# Assertions disabled for performance
# for tup in mgrs_indexers:
# # caller is responsible for ensuring this
diff --git a/pandas/core/internals/construction.py b/pandas/core/internals/construction.py
index 609d2c9a7a285..ff5e8e35f92ab 100644
--- a/pandas/core/internals/construction.py
+++ b/pandas/core/internals/construction.py
@@ -46,7 +46,6 @@
from pandas.core.arrays.string_ import StringDtype
from pandas.core.construction import (
array as pd_array,
- ensure_wrapped_if_datetimelike,
extract_array,
range_to_ndarray,
sanitize_array,
@@ -60,10 +59,6 @@
get_objs_combined_axis,
union_indexes,
)
-from pandas.core.internals.array_manager import (
- ArrayManager,
- SingleArrayManager,
-)
from pandas.core.internals.blocks import (
BlockPlacement,
ensure_block_shape,
@@ -71,8 +66,6 @@
new_block_2d,
)
from pandas.core.internals.managers import (
- BlockManager,
- SingleBlockManager,
create_block_manager_from_blocks,
create_block_manager_from_column_arrays,
)
@@ -100,7 +93,6 @@ def arrays_to_mgr(
*,
dtype: DtypeObj | None = None,
verify_integrity: bool = True,
- typ: str | None = None,
consolidate: bool = True,
) -> Manager:
"""
@@ -148,14 +140,9 @@ def arrays_to_mgr(
# from BlockManager perspective
axes = [columns, index]
- if typ == "block":
- return create_block_manager_from_column_arrays(
- arrays, axes, consolidate=consolidate, refs=refs
- )
- elif typ == "array":
- return ArrayManager(arrays, [index, columns])
- else:
- raise ValueError(f"'typ' needs to be one of {{'block', 'array'}}, got '{typ}'")
+ return create_block_manager_from_column_arrays(
+ arrays, axes, consolidate=consolidate, refs=refs
+ )
def rec_array_to_mgr(
@@ -164,7 +151,6 @@ def rec_array_to_mgr(
columns,
dtype: DtypeObj | None,
copy: bool,
- typ: str,
) -> Manager:
"""
Extract from a masked rec array and create the manager.
@@ -186,56 +172,19 @@ def rec_array_to_mgr(
if columns is None:
columns = arr_columns
- mgr = arrays_to_mgr(arrays, columns, index, dtype=dtype, typ=typ)
+ mgr = arrays_to_mgr(arrays, columns, index, dtype=dtype)
if copy:
mgr = mgr.copy()
return mgr
-def mgr_to_mgr(mgr, typ: str, copy: bool = True) -> Manager:
- """
- Convert to specific type of Manager. Does not copy if the type is already
- correct. Does not guarantee a copy otherwise. `copy` keyword only controls
- whether conversion from Block->ArrayManager copies the 1D arrays.
- """
- new_mgr: Manager
-
- if typ == "block":
- if isinstance(mgr, BlockManager):
- new_mgr = mgr
- else:
- if mgr.ndim == 2:
- new_mgr = arrays_to_mgr(
- mgr.arrays, mgr.axes[0], mgr.axes[1], typ="block"
- )
- else:
- new_mgr = SingleBlockManager.from_array(mgr.arrays[0], mgr.index)
- elif typ == "array":
- if isinstance(mgr, ArrayManager):
- new_mgr = mgr
- else:
- if mgr.ndim == 2:
- arrays = [mgr.iget_values(i) for i in range(len(mgr.axes[0]))]
- if copy:
- arrays = [arr.copy() for arr in arrays]
- new_mgr = ArrayManager(arrays, [mgr.axes[1], mgr.axes[0]])
- else:
- array = mgr.internal_values()
- if copy:
- array = array.copy()
- new_mgr = SingleArrayManager([array], [mgr.index])
- else:
- raise ValueError(f"'typ' needs to be one of {{'block', 'array'}}, got '{typ}'")
- return new_mgr
-
-
# ---------------------------------------------------------------------
# DataFrame Constructor Interface
def ndarray_to_mgr(
- values, index, columns, dtype: DtypeObj | None, copy: bool, typ: str
+ values, index, columns, dtype: DtypeObj | None, copy: bool
) -> Manager:
# used in DataFrame.__init__
# input must be a ndarray, list, Series, Index, ExtensionArray
@@ -253,10 +202,6 @@ def ndarray_to_mgr(
if not len(values) and columns is not None and len(columns):
values = np.empty((0, 1), dtype=object)
- # if the array preparation does a copy -> avoid this for ArrayManager,
- # since the copy is done on conversion to 1D arrays
- copy_on_sanitize = False if typ == "array" else copy
-
vdtype = getattr(values, "dtype", None)
refs = None
if is_1d_only_ea_dtype(vdtype) or is_1d_only_ea_dtype(dtype):
@@ -279,7 +224,7 @@ def ndarray_to_mgr(
else:
columns = ensure_index(columns)
- return arrays_to_mgr(values, columns, index, dtype=dtype, typ=typ)
+ return arrays_to_mgr(values, columns, index, dtype=dtype)
elif isinstance(vdtype, ExtensionDtype):
# i.e. Datetime64TZ, PeriodDtype; cases with is_1d_only_ea_dtype(vdtype)
@@ -291,12 +236,10 @@ def ndarray_to_mgr(
values = values.reshape(-1, 1)
elif isinstance(values, (ABCSeries, Index)):
- if not copy_on_sanitize and (
- dtype is None or astype_is_view(values.dtype, dtype)
- ):
+ if not copy and (dtype is None or astype_is_view(values.dtype, dtype)):
refs = values._references
- if copy_on_sanitize:
+ if copy:
values = values._values.copy()
else:
values = values._values
@@ -306,9 +249,7 @@ def ndarray_to_mgr(
elif isinstance(values, (np.ndarray, ExtensionArray)):
# drop subclass info
_copy = (
- copy_on_sanitize
- if (dtype is None or astype_is_view(values.dtype, dtype))
- else False
+ copy if (dtype is None or astype_is_view(values.dtype, dtype)) else False
)
values = np.array(values, copy=_copy)
values = _ensure_2d(values)
@@ -316,7 +257,7 @@ def ndarray_to_mgr(
else:
# by definition an array here
# the dtypes will be coerced to a single dtype
- values = _prep_ndarraylike(values, copy=copy_on_sanitize)
+ values = _prep_ndarraylike(values, copy=copy)
if dtype is not None and values.dtype != dtype:
# GH#40110 see similar check inside sanitize_array
@@ -324,7 +265,7 @@ def ndarray_to_mgr(
values,
None,
dtype=dtype,
- copy=copy_on_sanitize,
+ copy=copy,
allow_2d=True,
)
@@ -335,27 +276,6 @@ def ndarray_to_mgr(
_check_values_indices_shape_match(values, index, columns)
- if typ == "array":
- if issubclass(values.dtype.type, str):
- values = np.array(values, dtype=object)
-
- if dtype is None and is_object_dtype(values.dtype):
- arrays = [
- ensure_wrapped_if_datetimelike(
- maybe_infer_to_datetimelike(values[:, i])
- )
- for i in range(values.shape[1])
- ]
- else:
- if lib.is_np_dtype(values.dtype, "mM"):
- values = ensure_wrapped_if_datetimelike(values)
- arrays = [values[:, i] for i in range(values.shape[1])]
-
- if copy:
- arrays = [arr.copy() for arr in arrays]
-
- return ArrayManager(arrays, [index, columns], verify_integrity=False)
-
values = values.T
# if we don't have a dtype specified, then try to convert objects
@@ -426,7 +346,6 @@ def dict_to_mgr(
columns,
*,
dtype: DtypeObj | None = None,
- typ: str = "block",
copy: bool = True,
) -> Manager:
"""
@@ -481,26 +400,22 @@ def dict_to_mgr(
arrays = [com.maybe_iterable_to_list(data[k]) for k in keys]
if copy:
- if typ == "block":
- # We only need to copy arrays that will not get consolidated, i.e.
- # only EA arrays
- arrays = [
- x.copy()
- if isinstance(x, ExtensionArray)
- else x.copy(deep=True)
- if (
- isinstance(x, Index)
- or isinstance(x, ABCSeries)
- and is_1d_only_ea_dtype(x.dtype)
- )
- else x
- for x in arrays
- ]
- else:
- # dtype check to exclude e.g. range objects, scalars
- arrays = [x.copy() if hasattr(x, "dtype") else x for x in arrays]
+ # We only need to copy arrays that will not get consolidated, i.e.
+ # only EA arrays
+ arrays = [
+ x.copy()
+ if isinstance(x, ExtensionArray)
+ else x.copy(deep=True)
+ if (
+ isinstance(x, Index)
+ or isinstance(x, ABCSeries)
+ and is_1d_only_ea_dtype(x.dtype)
+ )
+ else x
+ for x in arrays
+ ]
- return arrays_to_mgr(arrays, columns, index, dtype=dtype, typ=typ, consolidate=copy)
+ return arrays_to_mgr(arrays, columns, index, dtype=dtype, consolidate=copy)
def nested_data_to_arrays(
diff --git a/pandas/core/internals/managers.py b/pandas/core/internals/managers.py
index d08dee3663395..c6a8b61e0c51e 100644
--- a/pandas/core/internals/managers.py
+++ b/pandas/core/internals/managers.py
@@ -367,9 +367,6 @@ def apply(
out = type(self).from_blocks(result_blocks, self.axes)
return out
- # Alias so we can share code with ArrayManager
- apply_with_block = apply
-
def setitem(self, indexer, value, warn: bool = True) -> Self:
"""
Set values with indexer.
@@ -2025,7 +2022,7 @@ def setitem_inplace(self, indexer, value, warn: bool = True) -> None:
"""
Set values with indexer.
- For Single[Block/Array]Manager, this backs s[indexer] = value
+ For SingleBlockManager, this backs s[indexer] = value
This is an inplace version of `setitem()`, mutating the manager/values
in place, not returning a new Manager (and Block), and thus never changing
diff --git a/pandas/core/series.py b/pandas/core/series.py
index 19e54954bb41c..7b56facc7a91f 100644
--- a/pandas/core/series.py
+++ b/pandas/core/series.py
@@ -30,7 +30,6 @@
using_copy_on_write,
warn_copy_on_write,
)
-from pandas._config.config import _get_option
from pandas._libs import (
lib,
@@ -150,10 +149,7 @@
check_bool_indexer,
check_dict_or_set_indexers,
)
-from pandas.core.internals import (
- SingleArrayManager,
- SingleBlockManager,
-)
+from pandas.core.internals import SingleBlockManager
from pandas.core.methods import selectn
from pandas.core.shared_docs import _shared_docs
from pandas.core.sorting import (
@@ -200,7 +196,6 @@
Renamer,
Scalar,
Self,
- SingleManager,
SortKind,
StorageOptions,
Suffixes,
@@ -385,7 +380,7 @@ class Series(base.IndexOpsMixin, NDFrame): # type: ignore[misc]
base.IndexOpsMixin.hasnans.fget, # type: ignore[attr-defined]
doc=base.IndexOpsMixin.hasnans.__doc__,
)
- _mgr: SingleManager
+ _mgr: SingleBlockManager
# ----------------------------------------------------------------------
# Constructors
@@ -411,7 +406,7 @@ def __init__(
allow_mgr = False
if (
- isinstance(data, (SingleBlockManager, SingleArrayManager))
+ isinstance(data, SingleBlockManager)
and index is None
and dtype is None
and (copy is False or copy is None)
@@ -450,12 +445,8 @@ def __init__(
# we are called internally, so short-circuit
if fastpath:
# data is a ndarray, index is defined
- if not isinstance(data, (SingleBlockManager, SingleArrayManager)):
- manager = _get_option("mode.data_manager", silent=True)
- if manager == "block":
- data = SingleBlockManager.from_array(data, index)
- elif manager == "array":
- data = SingleArrayManager.from_array(data, index)
+ if not isinstance(data, SingleBlockManager):
+ data = SingleBlockManager.from_array(data, index)
allow_mgr = True
elif using_copy_on_write() and not copy:
data = data.copy(deep=False)
@@ -541,7 +532,7 @@ def __init__(
data, index = self._init_dict(data, index, dtype)
dtype = None
copy = False
- elif isinstance(data, (SingleBlockManager, SingleArrayManager)):
+ elif isinstance(data, SingleBlockManager):
if index is None:
index = data.index
elif not data.index.equals(index) or copy:
@@ -579,19 +570,14 @@ def __init__(
com.require_length_match(data, index)
# create/copy the manager
- if isinstance(data, (SingleBlockManager, SingleArrayManager)):
+ if isinstance(data, SingleBlockManager):
if dtype is not None:
data = data.astype(dtype=dtype, errors="ignore", copy=copy)
elif copy:
data = data.copy()
else:
data = sanitize_array(data, index, dtype, copy)
-
- manager = _get_option("mode.data_manager", silent=True)
- if manager == "block":
- data = SingleBlockManager.from_array(data, index, refs=refs)
- elif manager == "array":
- data = SingleArrayManager.from_array(data, index)
+ data = SingleBlockManager.from_array(data, index, refs=refs)
NDFrame.__init__(self, data)
self.name = name
@@ -863,9 +849,7 @@ def _values(self):
return self._mgr.internal_values()
@property
- def _references(self) -> BlockValuesRefs | None:
- if isinstance(self._mgr, SingleArrayManager):
- return None
+ def _references(self) -> BlockValuesRefs:
return self._mgr._block.refs
# error: Decorated property not supported
@@ -969,7 +953,6 @@ def view(self, dtype: Dtype | None = None) -> Series:
res_ser = self._constructor(res_values, index=self.index, copy=False)
if isinstance(res_ser._mgr, SingleBlockManager):
blk = res_ser._mgr._block
- blk.refs = cast("BlockValuesRefs", self._references)
blk.refs.add_reference(blk)
return res_ser.__finalize__(self, method="view")
@@ -1205,7 +1188,7 @@ def _get_values_tuple(self, key: tuple):
indexer, new_index = self.index.get_loc_level(key)
new_ser = self._constructor(self._values[indexer], index=new_index, copy=False)
if isinstance(indexer, slice):
- new_ser._mgr.add_references(self._mgr) # type: ignore[arg-type]
+ new_ser._mgr.add_references(self._mgr)
return new_ser.__finalize__(self)
def _get_rows_with_mask(self, indexer: npt.NDArray[np.bool_]) -> Series:
@@ -1247,7 +1230,7 @@ def _get_value(self, label, takeable: bool = False):
new_values, index=new_index, name=self.name, copy=False
)
if isinstance(loc, slice):
- new_ser._mgr.add_references(self._mgr) # type: ignore[arg-type]
+ new_ser._mgr.add_references(self._mgr)
return new_ser.__finalize__(self)
else:
@@ -1270,7 +1253,7 @@ def __setitem__(self, key, value) -> None:
warn_copy_on_write()
or (
not warn_copy_on_write()
- and self._mgr.blocks[0].refs.has_reference() # type: ignore[union-attr]
+ and self._mgr.blocks[0].refs.has_reference()
)
):
warn = False
diff --git a/pandas/io/parquet.py b/pandas/io/parquet.py
index 5d0bc3f2c159b..e04a3acc829f1 100644
--- a/pandas/io/parquet.py
+++ b/pandas/io/parquet.py
@@ -13,7 +13,6 @@
from warnings import catch_warnings
from pandas._config import using_pyarrow_string_dtype
-from pandas._config.config import _get_option
from pandas._libs import lib
from pandas.compat._optional import import_optional_dependency
@@ -260,10 +259,6 @@ def read(
elif using_pyarrow_string_dtype():
to_pandas_kwargs["types_mapper"] = arrow_string_types_mapper()
- manager = _get_option("mode.data_manager", silent=True)
- if manager == "array":
- to_pandas_kwargs["split_blocks"] = True # type: ignore[assignment]
-
path_or_handle, handles, filesystem = _get_path_or_handle(
path,
filesystem,
@@ -280,9 +275,6 @@ def read(
)
result = pa_table.to_pandas(**to_pandas_kwargs)
- if manager == "array":
- result = result._as_manager("array", copy=False)
-
if pa_table.schema.metadata:
if b"PANDAS_ATTRS" in pa_table.schema.metadata:
df_metadata = pa_table.schema.metadata[b"PANDAS_ATTRS"]
diff --git a/pandas/io/pytables.py b/pandas/io/pytables.py
index 3d6d1c213ef10..0baf642495584 100644
--- a/pandas/io/pytables.py
+++ b/pandas/io/pytables.py
@@ -92,10 +92,6 @@
)
from pandas.core.construction import extract_array
from pandas.core.indexes.api import ensure_index
-from pandas.core.internals import (
- ArrayManager,
- BlockManager,
-)
from pandas.io.common import stringify_path
from pandas.io.formats.printing import (
@@ -3311,10 +3307,6 @@ def read(
def write(self, obj, **kwargs) -> None:
super().write(obj, **kwargs)
- # TODO(ArrayManager) HDFStore relies on accessing the blocks
- if isinstance(obj._mgr, ArrayManager):
- obj = obj._as_manager("block")
-
data = obj._mgr
if not data.is_consolidated():
data = data.consolidate()
@@ -4123,16 +4115,10 @@ def _get_blocks_and_items(
data_columns,
):
# Helper to clarify non-state-altering parts of _create_axes
-
- # TODO(ArrayManager) HDFStore relies on accessing the blocks
- if isinstance(frame._mgr, ArrayManager):
- frame = frame._as_manager("block")
-
def get_blk_items(mgr):
return [mgr.items.take(blk.mgr_locs) for blk in mgr.blocks]
mgr = frame._mgr
- mgr = cast(BlockManager, mgr)
blocks: list[Block] = list(mgr.blocks)
blk_items: list[Index] = get_blk_items(mgr)
@@ -4144,7 +4130,6 @@ def get_blk_items(mgr):
axis, axis_labels = new_non_index_axes[0]
new_labels = Index(axis_labels).difference(Index(data_columns))
mgr = frame.reindex(new_labels, axis=axis)._mgr
- mgr = cast(BlockManager, mgr)
blocks = list(mgr.blocks)
blk_items = get_blk_items(mgr)
@@ -4153,7 +4138,6 @@ def get_blk_items(mgr):
# index, so we can infer that (as long as axis==1) we
# get a single column back, so a single block.
mgr = frame.reindex([c], axis=axis)._mgr
- mgr = cast(BlockManager, mgr)
blocks.extend(mgr.blocks)
blk_items.extend(get_blk_items(mgr))
diff --git a/pandas/io/stata.py b/pandas/io/stata.py
index 2ac49600d4d7e..15ef20e9f453e 100644
--- a/pandas/io/stata.py
+++ b/pandas/io/stata.py
@@ -1853,10 +1853,6 @@ def _do_convert_missing(self, data: DataFrame, convert_missing: bool) -> DataFra
if dtype not in (np.float32, np.float64):
dtype = np.float64
replacement = Series(series, dtype=dtype)
- if not replacement._values.flags["WRITEABLE"]:
- # only relevant for ArrayManager; construction
- # path for BlockManager ensures writeability
- replacement = replacement.copy()
# Note: operating on ._values is much faster than directly
# TODO: can we fix that?
replacement._values[missing] = np.nan
diff --git a/pandas/tests/apply/test_frame_apply.py b/pandas/tests/apply/test_frame_apply.py
index 0839f005305a5..66a43f2ba4bcd 100644
--- a/pandas/tests/apply/test_frame_apply.py
+++ b/pandas/tests/apply/test_frame_apply.py
@@ -1507,8 +1507,6 @@ def func(row):
tm.assert_frame_equal(result, expected)
if using_copy_on_write:
# INFO(CoW) With copy on write, mutating a viewing row doesn't mutate the parent
- # INFO(ArrayManager) With BlockManager, the row is a view and mutated in place,
- # with ArrayManager the row is not a view, and thus not mutated in place
tm.assert_frame_equal(df, df_orig)
else:
tm.assert_frame_equal(df, result)
diff --git a/pandas/tests/copy_view/test_indexing.py b/pandas/tests/copy_view/test_indexing.py
index 72cb410c9068d..c35a0b89585c3 100644
--- a/pandas/tests/copy_view/test_indexing.py
+++ b/pandas/tests/copy_view/test_indexing.py
@@ -215,7 +215,7 @@ def test_subset_loc_rows_columns(
subset = df.loc[row_indexer, column_indexer]
# a few corner cases _do_ actually modify the parent (with both row and column
- # slice, and in case of ArrayManager or BlockManager with single block)
+ # slice, and in case of BlockManager with single block)
mutate_parent = (
isinstance(row_indexer, slice)
and isinstance(column_indexer, slice)
@@ -271,7 +271,7 @@ def test_subset_iloc_rows_columns(
subset = df.iloc[row_indexer, column_indexer]
# a few corner cases _do_ actually modify the parent (with both row and column
- # slice, and in case of ArrayManager or BlockManager with single block)
+ # slice, and in case of BlockManager with single block)
mutate_parent = (
isinstance(row_indexer, slice)
and isinstance(column_indexer, slice)
diff --git a/pandas/tests/extension/base/setitem.py b/pandas/tests/extension/base/setitem.py
index 7ee2c23c5b23a..f8bb0c92cf59e 100644
--- a/pandas/tests/extension/base/setitem.py
+++ b/pandas/tests/extension/base/setitem.py
@@ -414,7 +414,6 @@ def test_setitem_frame_2d_values(self, data):
tm.assert_frame_equal(df, orig)
if not using_copy_on_write:
# GH#33457 Check that this setting occurred in-place
- # FIXME(ArrayManager): this should work there too
assert df._mgr.arrays[0] is blk_data
df.iloc[:-1] = df.values[:-1]
diff --git a/pandas/tests/extension/conftest.py b/pandas/tests/extension/conftest.py
index c5b1295ee4a7d..77f1dd2a8e49c 100644
--- a/pandas/tests/extension/conftest.py
+++ b/pandas/tests/extension/conftest.py
@@ -2,8 +2,6 @@
import pytest
-from pandas._config.config import _get_option
-
from pandas import (
Series,
options,
@@ -224,7 +222,4 @@ def using_copy_on_write() -> bool:
"""
Fixture to check if Copy-on-Write is enabled.
"""
- return (
- options.mode.copy_on_write is True
- and _get_option("mode.data_manager", silent=True) == "block"
- )
+ return options.mode.copy_on_write is True
diff --git a/pandas/tests/frame/methods/test_astype.py b/pandas/tests/frame/methods/test_astype.py
index e7f2c410bf4ac..84e642f47417b 100644
--- a/pandas/tests/frame/methods/test_astype.py
+++ b/pandas/tests/frame/methods/test_astype.py
@@ -553,27 +553,11 @@ def test_astype_to_incorrect_datetimelike(self, unit):
other = f"m8[{unit}]"
df = DataFrame(np.array([[1, 2, 3]], dtype=dtype))
- msg = "|".join(
- [
- # BlockManager path
- rf"Cannot cast DatetimeArray to dtype timedelta64\[{unit}\]",
- # ArrayManager path
- "cannot astype a datetimelike from "
- rf"\[datetime64\[ns\]\] to \[timedelta64\[{unit}\]\]",
- ]
- )
+ msg = rf"Cannot cast DatetimeArray to dtype timedelta64\[{unit}\]"
with pytest.raises(TypeError, match=msg):
df.astype(other)
- msg = "|".join(
- [
- # BlockManager path
- rf"Cannot cast TimedeltaArray to dtype datetime64\[{unit}\]",
- # ArrayManager path
- "cannot astype a timedelta from "
- rf"\[timedelta64\[ns\]\] to \[datetime64\[{unit}\]\]",
- ]
- )
+ msg = rf"Cannot cast TimedeltaArray to dtype datetime64\[{unit}\]"
df = DataFrame(np.array([[1, 2, 3]], dtype=other))
with pytest.raises(TypeError, match=msg):
df.astype(dtype)
diff --git a/pandas/tests/frame/test_constructors.py b/pandas/tests/frame/test_constructors.py
index c66b6a0f8b99b..9ff2b52bd35ff 100644
--- a/pandas/tests/frame/test_constructors.py
+++ b/pandas/tests/frame/test_constructors.py
@@ -311,8 +311,6 @@ def test_constructor_dtype_nocast_view_2d_array(
should_be_view.iloc[0, 0] = 97
assert df.values[0, 0] == 97
else:
- # INFO(ArrayManager) DataFrame(ndarray) doesn't necessarily preserve
- # a view on the array to ensure contiguous 1D arrays
df2 = DataFrame(df.values, dtype=df[0].dtype)
assert df2._mgr.arrays[0].flags.c_contiguous
@@ -2504,8 +2502,6 @@ def get_base(obj):
raise TypeError
def check_views(c_only: bool = False):
- # written to work for either BlockManager or ArrayManager
-
# Check that the underlying data behind df["c"] is still `c`
# after setting with iloc. Since we don't know which entry in
# df._mgr.arrays corresponds to df["c"], we just check that exactly
diff --git a/pandas/tests/frame/test_reductions.py b/pandas/tests/frame/test_reductions.py
index fa233619ad3a3..63c15fab76562 100644
--- a/pandas/tests/frame/test_reductions.py
+++ b/pandas/tests/frame/test_reductions.py
@@ -1180,10 +1180,7 @@ def test_idxmax_axis_2(self, float_frame):
def test_idxmax_mixed_dtype(self):
# don't cast to object, which would raise in nanops
dti = date_range("2016-01-01", periods=3)
-
- # Copying dti is needed for ArrayManager otherwise when we set
- # df.loc[0, 3] = pd.NaT below it edits dti
- df = DataFrame({1: [0, 2, 1], 2: range(3)[::-1], 3: dti.copy(deep=True)})
+ df = DataFrame({1: [0, 2, 1], 2: range(3)[::-1], 3: dti})
result = df.idxmax()
expected = Series([1, 0, 2], index=[1, 2, 3])
diff --git a/pandas/tests/indexing/test_chaining_and_caching.py b/pandas/tests/indexing/test_chaining_and_caching.py
index 5eeaa50e2c3b6..5acfb72c4a666 100644
--- a/pandas/tests/indexing/test_chaining_and_caching.py
+++ b/pandas/tests/indexing/test_chaining_and_caching.py
@@ -51,7 +51,6 @@ def test_slice_consolidate_invalidate_item_cache(self, using_copy_on_write):
if not using_copy_on_write:
tm.assert_almost_equal(df["bb"][0], 0.17)
else:
- # with ArrayManager, parent is not mutated with chained assignment
tm.assert_almost_equal(df["bb"][0], 2.2)
@pytest.mark.parametrize("do_ref", [True, False])
diff --git a/pandas/tests/internals/test_api.py b/pandas/tests/internals/test_api.py
index 1251a6ae97a1c..ccd7222fb16e1 100644
--- a/pandas/tests/internals/test_api.py
+++ b/pandas/tests/internals/test_api.py
@@ -23,7 +23,6 @@ def test_namespace():
"concat",
"managers",
"construction",
- "array_manager",
"base",
"api",
"ops",
@@ -31,11 +30,9 @@ def test_namespace():
expected = [
"make_block",
"DataManager",
- "ArrayManager",
"BlockManager",
"SingleDataManager",
"SingleBlockManager",
- "SingleArrayManager",
"concatenate_managers",
]
diff --git a/pandas/tests/internals/test_managers.py b/pandas/tests/internals/test_managers.py
deleted file mode 100644
index f40362c299717..0000000000000
--- a/pandas/tests/internals/test_managers.py
+++ /dev/null
@@ -1,103 +0,0 @@
-"""
-Testing interaction between the different managers (BlockManager, ArrayManager)
-"""
-import os
-import subprocess
-import sys
-
-import pytest
-
-from pandas.core.dtypes.missing import array_equivalent
-
-import pandas as pd
-import pandas._testing as tm
-from pandas.core.internals import (
- ArrayManager,
- BlockManager,
- SingleArrayManager,
- SingleBlockManager,
-)
-
-
-def test_dataframe_creation():
- msg = "data_manager option is deprecated"
- with tm.assert_produces_warning(FutureWarning, match=msg):
- with pd.option_context("mode.data_manager", "block"):
- df_block = pd.DataFrame(
- {"a": [1, 2, 3], "b": [0.1, 0.2, 0.3], "c": [4, 5, 6]}
- )
- assert isinstance(df_block._mgr, BlockManager)
-
- with tm.assert_produces_warning(FutureWarning, match=msg):
- with pd.option_context("mode.data_manager", "array"):
- df_array = pd.DataFrame(
- {"a": [1, 2, 3], "b": [0.1, 0.2, 0.3], "c": [4, 5, 6]}
- )
- assert isinstance(df_array._mgr, ArrayManager)
-
- # also ensure both are seen as equal
- tm.assert_frame_equal(df_block, df_array)
-
- # conversion from one manager to the other
- result = df_block._as_manager("block")
- assert isinstance(result._mgr, BlockManager)
- result = df_block._as_manager("array")
- assert isinstance(result._mgr, ArrayManager)
- tm.assert_frame_equal(result, df_block)
- assert all(
- array_equivalent(left, right)
- for left, right in zip(result._mgr.arrays, df_array._mgr.arrays)
- )
-
- result = df_array._as_manager("array")
- assert isinstance(result._mgr, ArrayManager)
- result = df_array._as_manager("block")
- assert isinstance(result._mgr, BlockManager)
- tm.assert_frame_equal(result, df_array)
- assert len(result._mgr.blocks) == 2
-
-
-def test_series_creation():
- msg = "data_manager option is deprecated"
- with tm.assert_produces_warning(FutureWarning, match=msg):
- with pd.option_context("mode.data_manager", "block"):
- s_block = pd.Series([1, 2, 3], name="A", index=["a", "b", "c"])
- assert isinstance(s_block._mgr, SingleBlockManager)
-
- with tm.assert_produces_warning(FutureWarning, match=msg):
- with pd.option_context("mode.data_manager", "array"):
- s_array = pd.Series([1, 2, 3], name="A", index=["a", "b", "c"])
- assert isinstance(s_array._mgr, SingleArrayManager)
-
- # also ensure both are seen as equal
- tm.assert_series_equal(s_block, s_array)
-
- # conversion from one manager to the other
- result = s_block._as_manager("block")
- assert isinstance(result._mgr, SingleBlockManager)
- result = s_block._as_manager("array")
- assert isinstance(result._mgr, SingleArrayManager)
- tm.assert_series_equal(result, s_block)
-
- result = s_array._as_manager("array")
- assert isinstance(result._mgr, SingleArrayManager)
- result = s_array._as_manager("block")
- assert isinstance(result._mgr, SingleBlockManager)
- tm.assert_series_equal(result, s_array)
-
-
-@pytest.mark.single_cpu
-@pytest.mark.parametrize("manager", ["block", "array"])
-def test_array_manager_depr_env_var(manager):
- # GH#55043
- test_env = os.environ.copy()
- test_env["PANDAS_DATA_MANAGER"] = manager
- response = subprocess.run(
- [sys.executable, "-c", "import pandas"],
- capture_output=True,
- env=test_env,
- check=True,
- )
- msg = "FutureWarning: The env variable PANDAS_DATA_MANAGER is set"
- stderr_msg = response.stderr.decode("utf-8")
- assert msg in stderr_msg, stderr_msg
diff --git a/pandas/tests/io/test_parquet.py b/pandas/tests/io/test_parquet.py
index 83a962ec26a7e..bba53f7ff50a8 100644
--- a/pandas/tests/io/test_parquet.py
+++ b/pandas/tests/io/test_parquet.py
@@ -9,7 +9,6 @@
import pytest
from pandas._config import using_copy_on_write
-from pandas._config.config import _get_option
from pandas.compat import is_platform_windows
from pandas.compat.pyarrow import (
@@ -45,8 +44,6 @@
_HAVE_FASTPARQUET = False
-# TODO(ArrayManager) fastparquet relies on BlockManager internals
-
pytestmark = [
pytest.mark.filterwarnings("ignore:DataFrame._data is deprecated:FutureWarning"),
pytest.mark.filterwarnings(
@@ -61,9 +58,8 @@
pytest.param(
"fastparquet",
marks=pytest.mark.skipif(
- not _HAVE_FASTPARQUET
- or _get_option("mode.data_manager", silent=True) == "array",
- reason="fastparquet is not installed or ArrayManager is used",
+ not _HAVE_FASTPARQUET,
+ reason="fastparquet is not installed",
),
),
pytest.param(
@@ -89,8 +85,6 @@ def pa():
def fp():
if not _HAVE_FASTPARQUET:
pytest.skip("fastparquet is not installed")
- elif _get_option("mode.data_manager", silent=True) == "array":
- pytest.skip("ArrayManager is not supported with fastparquet")
return "fastparquet"
@@ -997,17 +991,6 @@ def test_filter_row_groups(self, pa):
result = read_parquet(path, pa, filters=[("a", "==", 0)])
assert len(result) == 1
- def test_read_parquet_manager(self, pa):
- # ensure that read_parquet honors the pandas.options.mode.data_manager option
- df = pd.DataFrame(
- np.random.default_rng(2).standard_normal((10, 3)), columns=["A", "B", "C"]
- )
-
- with tm.ensure_clean() as path:
- df.to_parquet(path, engine=pa)
- result = read_parquet(path, pa)
- assert isinstance(result._mgr, pd.core.internals.BlockManager)
-
def test_read_dtype_backend_pyarrow_config(self, pa, df_full):
import pyarrow
diff --git a/scripts/validate_unwanted_patterns.py b/scripts/validate_unwanted_patterns.py
index 698268ce382f8..6f36f7f47cb88 100755
--- a/scripts/validate_unwanted_patterns.py
+++ b/scripts/validate_unwanted_patterns.py
@@ -56,7 +56,7 @@
"_version_meson",
# The numba extensions need this to mock the iloc object
"_iLocIndexer",
- # TODO(3.0): GH#55043 - remove upon removal of ArrayManager
+ # TODO(4.0): GH#55043 - remove upon removal of CoW option
"_get_option",
"_fill_limit_area_1d",
}
| - [ ] closes #xxxx (Replace xxxx with the GitHub issue number)
- [ ] [Tests added and passed](https://pandas.pydata.org/pandas-docs/dev/development/contributing_codebase.html#writing-tests) if fixing a bug or adding a new feature
- [ ] All [code checks passed](https://pandas.pydata.org/pandas-docs/dev/development/contributing_codebase.html#pre-commit).
- [ ] Added [type annotations](https://pandas.pydata.org/pandas-docs/dev/development/contributing_codebase.html#type-hints) to new arguments/methods/functions.
- [ ] Added an entry in the latest `doc/source/whatsnew/vX.X.X.rst` file if fixing a bug or adding a new feature.
| https://api.github.com/repos/pandas-dev/pandas/pulls/57118 | 2024-01-28T18:24:36Z | 2024-01-31T23:34:04Z | 2024-01-31T23:34:04Z | 2024-01-31T23:39:05Z |
DOC: fix PR02 errors in docstrings - pandas.describe_option, pandas.get_option, pandas.reset_option | diff --git a/ci/code_checks.sh b/ci/code_checks.sh
index c0a26edd230eb..484cde210cb95 100755
--- a/ci/code_checks.sh
+++ b/ci/code_checks.sh
@@ -162,9 +162,6 @@ if [[ -z "$CHECK" || "$CHECK" == "docstrings" ]]; then
pandas.tseries.offsets.Milli\
pandas.tseries.offsets.Micro\
pandas.tseries.offsets.Nano\
- pandas.describe_option\
- pandas.reset_option\
- pandas.get_option\
pandas.set_option\
pandas.Timestamp.max\
pandas.Timestamp.min\
diff --git a/pandas/_config/config.py b/pandas/_config/config.py
index b74883dbe5734..3de0c3bac9c5f 100644
--- a/pandas/_config/config.py
+++ b/pandas/_config/config.py
@@ -54,6 +54,7 @@
ContextDecorator,
contextmanager,
)
+from inspect import signature
import re
from typing import (
TYPE_CHECKING,
@@ -270,6 +271,7 @@ class CallableDynamicDoc(Generic[T]):
def __init__(self, func: Callable[..., T], doc_tmpl: str) -> None:
self.__doc_tmpl__ = doc_tmpl
self.__func__ = func
+ self.__signature__ = signature(func)
def __call__(self, *args, **kwds) -> T:
return self.__func__(*args, **kwds)
| Test results:
Describe_option
```
$ scripts/validate_docstrings.py --format=actions --errors=PR02 pandas.describe_option
...
################################################################################
################################## Validation ##################################
################################################################################
1 Errors found for `pandas.describe_option`:
SA01 See Also section not found
```
Get_option
```
$ scripts/validate_docstrings.py --format=actions --errors=PR02 pandas.get_option
...
################################################################################
################################## Validation ##################################
################################################################################
2 Errors found for `pandas.get_option`:
PR01 Parameters {'silent'} not documented
SA01 See Also section not found
```
Reset_option
```
$ scripts/validate_docstrings.py --format=actions --errors=PR02 pandas.reset_option
...
################################################################################
################################## Validation ##################################
################################################################################
2 Errors found for `pandas.reset_option`:
PR01 Parameters {'silent'} not documented
SA01 See Also section not found
```
- [X] Cross-ref: https://github.com/pandas-dev/pandas/issues/57111
- [ ] [Tests added and passed](https://pandas.pydata.org/pandas-docs/dev/development/contributing_codebase.html#writing-tests) if fixing a bug or adding a new feature
- [ ] All [code checks passed](https://pandas.pydata.org/pandas-docs/dev/development/contributing_codebase.html#pre-commit).
- [ ] Added [type annotations](https://pandas.pydata.org/pandas-docs/dev/development/contributing_codebase.html#type-hints) to new arguments/methods/functions.
- [ ] Added an entry in the latest `doc/source/whatsnew/vX.X.X.rst` file if fixing a bug or adding a new feature.
| https://api.github.com/repos/pandas-dev/pandas/pulls/57117 | 2024-01-28T11:21:15Z | 2024-01-29T19:37:17Z | 2024-01-29T19:37:17Z | 2024-01-30T07:11:20Z |
BUG: fix `Series.value_counts` with `sort=False` returns result sorted on values for Series with string dtype | diff --git a/doc/source/whatsnew/v3.0.0.rst b/doc/source/whatsnew/v3.0.0.rst
index 73201fa93a8aa..ceaf3a953f046 100644
--- a/doc/source/whatsnew/v3.0.0.rst
+++ b/doc/source/whatsnew/v3.0.0.rst
@@ -152,7 +152,7 @@ Conversion
Strings
^^^^^^^
--
+- Bug in :meth:`Series.value_counts` would not respect ``sort=False`` for series having ``string`` dtype (:issue:`55224`)
-
Interval
diff --git a/pandas/core/arrays/string_.py b/pandas/core/arrays/string_.py
index b73b49eca3e18..6c4413052c12d 100644
--- a/pandas/core/arrays/string_.py
+++ b/pandas/core/arrays/string_.py
@@ -542,7 +542,7 @@ def max(self, axis=None, skipna: bool = True, **kwargs) -> Scalar:
def value_counts(self, dropna: bool = True) -> Series:
from pandas.core.algorithms import value_counts_internal as value_counts
- result = value_counts(self._ndarray, dropna=dropna).astype("Int64")
+ result = value_counts(self._ndarray, sort=False, dropna=dropna).astype("Int64")
result.index = result.index.astype(self.dtype)
return result
diff --git a/pandas/tests/arrays/string_/test_string.py b/pandas/tests/arrays/string_/test_string.py
index f67268616a021..cb324d29258c0 100644
--- a/pandas/tests/arrays/string_/test_string.py
+++ b/pandas/tests/arrays/string_/test_string.py
@@ -584,6 +584,19 @@ def test_value_counts_with_normalize(dtype):
tm.assert_series_equal(result, expected)
+def test_value_counts_sort_false(dtype):
+ if getattr(dtype, "storage", "") == "pyarrow":
+ exp_dtype = "int64[pyarrow]"
+ elif getattr(dtype, "storage", "") == "pyarrow_numpy":
+ exp_dtype = "int64"
+ else:
+ exp_dtype = "Int64"
+ ser = pd.Series(["a", "b", "c", "b"], dtype=dtype)
+ result = ser.value_counts(sort=False)
+ expected = pd.Series([1, 2, 1], index=ser[:3], dtype=exp_dtype, name="count")
+ tm.assert_series_equal(result, expected)
+
+
@pytest.mark.parametrize(
"values, expected",
[
|
- [x] closes #55224 (Replace xxxx with the GitHub issue number)
- [x] [Tests added and passed](https://pandas.pydata.org/pandas-docs/dev/development/contributing_codebase.html#writing-tests) if fixing a bug or adding a new feature
- [x] All [code checks passed](https://pandas.pydata.org/pandas-docs/dev/development/contributing_codebase.html#pre-commit).
- [x] Added [type annotations](https://pandas.pydata.org/pandas-docs/dev/development/contributing_codebase.html#type-hints) to new arguments/methods/functions.
- [x] Added an entry in the latest `doc/source/whatsnew/vX.X.X.rst` file if fixing a bug or adding a new feature.
| https://api.github.com/repos/pandas-dev/pandas/pulls/57116 | 2024-01-28T09:31:23Z | 2024-01-29T00:04:25Z | 2024-01-29T00:04:25Z | 2024-01-29T04:09:59Z |
fix: bug solution | diff --git a/pandas/tests/interchange/__init__.py b/pandas/tests/interchange/__init__.py
index e69de29bb2d1d..955d0955296c8 100644
--- a/pandas/tests/interchange/__init__.py
+++ b/pandas/tests/interchange/__init__.py
@@ -0,0 +1,11 @@
+import pandas as pd
+from datetime import datetime
+df = pd.Series(
+ [datetime(2000, 1, 1)], dtype="timestamp[ns][pyarrow]", name="col0"
+).to_frame()
+
+dfi = df.__dataframe__()
+result = pd.api.interchange.from_dataframe(dfi)
+
+print(result)
+# 1970-01-02 14:55:26.266485584
\ No newline at end of file
diff --git a/pandas/tests/interchange/test_impl.py b/pandas/tests/interchange/test_impl.py
index 96a239b89d6ce..170e6e5391fe5 100644
--- a/pandas/tests/interchange/test_impl.py
+++ b/pandas/tests/interchange/test_impl.py
@@ -248,6 +248,7 @@ def test_nonstring_object():
def test_datetime():
+ df = open("C:\\Users\\Bhavin Umatiya\\PycharmProjects\\github1\\pandas\\pandas\\tests\interchange\\__init__.py")
df = pd.DataFrame({"A": [pd.Timestamp("2022-01-01"), pd.NaT]})
col = df.__dataframe__().get_column_by_name("A")
diff --git a/pandas/tests/interchange/test_utils.py b/pandas/tests/interchange/test_utils.py
index a47bc2752ff32..5cdcbd5e00b81 100644
--- a/pandas/tests/interchange/test_utils.py
+++ b/pandas/tests/interchange/test_utils.py
@@ -84,6 +84,6 @@ def test_dtype_to_arrow_c_fmt_arrowdtype(pa_dtype, args_kwargs, c_string):
elif isinstance(args_kwargs, tuple):
pa_type = getattr(pa, pa_dtype)(*args_kwargs)
else:
- pa_type = getattr(pa, pa_dtype)(**args_kwargs)
+ pa_type = getattr(pa, pa_dtype)(*args_kwargs)
arrow_type = pd.ArrowDtype(pa_type)
assert dtype_to_arrow_c_fmt(arrow_type) == c_string
| - [ ] closes #xxxx (Replace xxxx with the GitHub issue number)
- [ ] [Tests added and passed](https://pandas.pydata.org/pandas-docs/dev/development/contributing_codebase.html#writing-tests) if fixing a bug or adding a new feature
- [ ] All [code checks passed](https://pandas.pydata.org/pandas-docs/dev/development/contributing_codebase.html#pre-commit).
- [ ] Added [type annotations](https://pandas.pydata.org/pandas-docs/dev/development/contributing_codebase.html#type-hints) to new arguments/methods/functions.
- [ ] Added an entry in the latest `doc/source/whatsnew/vX.X.X.rst` file if fixing a bug or adding a new feature.
| https://api.github.com/repos/pandas-dev/pandas/pulls/57115 | 2024-01-28T09:15:42Z | 2024-01-28T19:27:10Z | null | 2024-01-28T19:27:10Z |
DOC: fix PR02 errors in docstrings - pandas.Series.interpolate, pandas.read_hdf, pandas.HDFStore.append | diff --git a/ci/code_checks.sh b/ci/code_checks.sh
index c0a26edd230eb..a1ca16cfa444d 100755
--- a/ci/code_checks.sh
+++ b/ci/code_checks.sh
@@ -95,7 +95,6 @@ if [[ -z "$CHECK" || "$CHECK" == "docstrings" ]]; then
pandas.PeriodIndex.strftime\
pandas.Series.clip\
pandas.Series.rename_axis\
- pandas.Series.interpolate\
pandas.Series.dt.to_period\
pandas.Series.dt.tz_localize\
pandas.Series.dt.tz_convert\
@@ -186,8 +185,6 @@ if [[ -z "$CHECK" || "$CHECK" == "docstrings" ]]; then
pandas.core.groupby.DataFrameGroupBy.hist\
pandas.core.groupby.DataFrameGroupBy.plot\
pandas.core.groupby.SeriesGroupBy.plot\
- pandas.read_hdf\
- pandas.HDFStore.append\
pandas.core.window.rolling.Rolling.quantile\
pandas.core.window.expanding.Expanding.quantile\
pandas.api.extensions.ExtensionArray.argsort # There should be no backslash in the final line, please keep this comment in the last ignored function
diff --git a/pandas/io/pytables.py b/pandas/io/pytables.py
index 36c9b66f2bd47..3d6d1c213ef10 100644
--- a/pandas/io/pytables.py
+++ b/pandas/io/pytables.py
@@ -365,7 +365,7 @@ def read_hdf(
A list of Term (or convertible) objects.
start : int, optional
Row number to start selection.
- stop : int, optional
+ stop : int, optional
Row number to stop selection.
columns : list, optional
A list of columns names to return.
@@ -1269,7 +1269,7 @@ def append(
subsets of the data.
index : bool, default True
Write DataFrame index as a column.
- append : bool, default True
+ append : bool, default True
Append the input data to the existing.
data_columns : list of columns, or True, default None
List of columns to create as indexed data columns for on-disk
@@ -1277,10 +1277,10 @@ def append(
of the object are indexed. See `here
<https://pandas.pydata.org/pandas-docs/stable/user_guide/io.html#query-via-data-columns>`__.
min_itemsize : dict of columns that specify minimum str sizes
- nan_rep : str to use as str nan representation
- chunksize : size to chunk the writing
+ nan_rep : str to use as str nan representation
+ chunksize : size to chunk the writing
expectedrows : expected TOTAL row size of this table
- encoding : default None, provide an encoding for str
+ encoding : default None, provide an encoding for str
dropna : bool, default False, optional
Do not write an ALL nan row to the store settable
by the option 'io.hdf.dropna_table'.
| All PR02 Errors resolved in the following cases:
1. scripts/validate_docstrings.py --format=actions --errors=PR02 pandas.Series.interpolate
2. scripts/validate_docstrings.py --format=actions --errors=PR02 pandas.read_hdf
3. scripts/validate_docstrings.py --format=actions --errors=PR02 pandas.HDFStore.append
OUTPUT:
1. scripts/validate_docstrings.py --format=actions --errors=PR02 pandas.Series.interpolate
```
################################################################################
################################## Validation ##################################
################################################################################
Docstring for "pandas.Series.interpolate" correct. :)
```
2. scripts/validate_docstrings.py --format=actions --errors=PR02 pandas.read_hdf
```
################################################################################
################################## Validation ##################################
################################################################################
Docstring for "pandas.read_hdf" correct. :)
```
3. scripts/validate_docstrings.py --format=actions --errors=PR02 pandas.HDFStore.append
```
################################################################################
################################## Validation ##################################
################################################################################
8 Errors found for `pandas.HDFStore.append`:
PR01 Parameters {'complib', 'complevel', 'axes', 'columns', 'errors'} not documented
PR07 Parameter "key" has no description
PR07 Parameter "min_itemsize" has no description
PR07 Parameter "nan_rep" has no description
PR07 Parameter "chunksize" has no description
PR07 Parameter "expectedrows" has no description
PR07 Parameter "encoding" has no description
SA01 See Also section not found
```
- [x] xref https://github.com/pandas-dev/pandas/issues/57111
- [x] [Tests added and passed](https://pandas.pydata.org/pandas-docs/dev/development/contributing_codebase.html#writing-tests) if fixing a bug or adding a new feature
- [x] All [code checks passed](https://pandas.pydata.org/pandas-docs/dev/development/contributing_codebase.html#pre-commit).
- [ ] Added [type annotations](https://pandas.pydata.org/pandas-docs/dev/development/contributing_codebase.html#type-hints) to new arguments/methods/functions.
- [ ] Added an entry in the latest `doc/source/whatsnew/vX.X.X.rst` file if fixing a bug or adding a new feature.
| https://api.github.com/repos/pandas-dev/pandas/pulls/57114 | 2024-01-28T09:10:28Z | 2024-01-29T18:43:18Z | 2024-01-29T18:43:18Z | 2024-01-30T03:12:34Z |
bug solution | diff --git a/pandas/tests/interchange/__init__.py b/pandas/tests/interchange/__init__.py
index e69de29bb2d1d..955d0955296c8 100644
--- a/pandas/tests/interchange/__init__.py
+++ b/pandas/tests/interchange/__init__.py
@@ -0,0 +1,11 @@
+import pandas as pd
+from datetime import datetime
+df = pd.Series(
+ [datetime(2000, 1, 1)], dtype="timestamp[ns][pyarrow]", name="col0"
+).to_frame()
+
+dfi = df.__dataframe__()
+result = pd.api.interchange.from_dataframe(dfi)
+
+print(result)
+# 1970-01-02 14:55:26.266485584
\ No newline at end of file
diff --git a/pandas/tests/interchange/test_impl.py b/pandas/tests/interchange/test_impl.py
index 96a239b89d6ce..170e6e5391fe5 100644
--- a/pandas/tests/interchange/test_impl.py
+++ b/pandas/tests/interchange/test_impl.py
@@ -248,6 +248,7 @@ def test_nonstring_object():
def test_datetime():
+ df = open("C:\\Users\\Bhavin Umatiya\\PycharmProjects\\github1\\pandas\\pandas\\tests\interchange\\__init__.py")
df = pd.DataFrame({"A": [pd.Timestamp("2022-01-01"), pd.NaT]})
col = df.__dataframe__().get_column_by_name("A")
diff --git a/pandas/tests/interchange/test_utils.py b/pandas/tests/interchange/test_utils.py
index a47bc2752ff32..5cdcbd5e00b81 100644
--- a/pandas/tests/interchange/test_utils.py
+++ b/pandas/tests/interchange/test_utils.py
@@ -84,6 +84,6 @@ def test_dtype_to_arrow_c_fmt_arrowdtype(pa_dtype, args_kwargs, c_string):
elif isinstance(args_kwargs, tuple):
pa_type = getattr(pa, pa_dtype)(*args_kwargs)
else:
- pa_type = getattr(pa, pa_dtype)(**args_kwargs)
+ pa_type = getattr(pa, pa_dtype)(*args_kwargs)
arrow_type = pd.ArrowDtype(pa_type)
assert dtype_to_arrow_c_fmt(arrow_type) == c_string
| - [ ] closes #xxxx (Replace xxxx with the GitHub issue number)
- [ ] [Tests added and passed](https://pandas.pydata.org/pandas-docs/dev/development/contributing_codebase.html#writing-tests) if fixing a bug or adding a new feature
- [ ] All [code checks passed](https://pandas.pydata.org/pandas-docs/dev/development/contributing_codebase.html#pre-commit).
- [ ] Added [type annotations](https://pandas.pydata.org/pandas-docs/dev/development/contributing_codebase.html#type-hints) to new arguments/methods/functions.
- [ ] Added an entry in the latest `doc/source/whatsnew/vX.X.X.rst` file if fixing a bug or adding a new feature.
| https://api.github.com/repos/pandas-dev/pandas/pulls/57113 | 2024-01-28T09:03:12Z | 2024-01-28T09:03:46Z | null | 2024-01-28T09:11:31Z |
CI: Use new documentation previewer | diff --git a/.github/workflows/comment-commands.yml b/.github/workflows/comment-commands.yml
index 425abef850184..b24a689078d4e 100644
--- a/.github/workflows/comment-commands.yml
+++ b/.github/workflows/comment-commands.yml
@@ -24,12 +24,10 @@ jobs:
concurrency:
group: ${{ github.actor }}-preview-docs
steps:
- - run: |
- if curl --output /dev/null --silent --head --fail "https://pandas.pydata.org/preview/${{ github.event.issue.number }}/"; then
- curl -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" -d '{"body": "Website preview of this PR available at: https://pandas.pydata.org/preview/${{ github.event.issue.number }}/"}' https://api.github.com/repos/${{ github.repository }}/issues/${{ github.event.issue.number }}/comments
- else
- curl -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" -d '{"body": "No preview found for PR #${{ github.event.issue.number }}. Did the docs build complete?"}' https://api.github.com/repos/${{ github.repository }}/issues/${{ github.event.issue.number }}/comments
- fi
+ - uses: pandas-dev/github-doc-previewer@master
+ with:
+ previewer-server: "https://pandas.pydata.org/preview"
+ artifact-job: "Doc Build and Upload"
asv_run:
runs-on: ubuntu-22.04
# TODO: Support more benchmarking options later, against different branches, against self, etc
diff --git a/.github/workflows/docbuild-and-upload.yml b/.github/workflows/docbuild-and-upload.yml
index 73acd9acc129a..924a6263f34d2 100644
--- a/.github/workflows/docbuild-and-upload.yml
+++ b/.github/workflows/docbuild-and-upload.yml
@@ -90,10 +90,3 @@ jobs:
name: website
path: web/build
retention-days: 14
-
- - name: Trigger web/doc preview
- run: curl -X POST https://pandas.pydata.org/preview/submit/$RUN_ID/$PR_ID/
- env:
- RUN_ID: ${{ github.run_id }}
- PR_ID: ${{ github.event.pull_request.number }}
- if: github.event_name == 'pull_request'
| - [X] closes #50832
We've got a documentation previewer service for few months now, but the whole infrastructure powering it was just a PoC not very suitable for the long term (it didn't have error control, was running in a screen session, the workflow wasn't optimal, a github token was hardcoded, it required to remove old previews manually...).
Now we've got a service that IMHO is ready for production. Everything is built independently from pandas in this repo:
https://github.com/pandas-dev/github-doc-previewer/. In the pandas side the only thing we need is to call a GitHub action (check the diff in this PR).
For most people there won't be a significant difference between the previous PoC and the new system, as the UX is the same. Besides stability the main differences are:
- A preview will be generated only for the PRs where it's requested (with the `/preview` comment). Previously we published the previews of every PR.
- The new service is pandas-agnostic. If there is interest, any other repo can use it, the only thing needed is that the GitHub organization of the project is added to a setting in the server. The whole `pandas-dev` organization is of course added. Not sure if we have any other repo where this can be useful, but only adding the GitHub action is enough to have previews enabled.
- The previous point has a small implication. The urls of the previews now contain the org and repo. So, instead of pandas.pydata.org/preview/1234/ the new url is now pandas.pydata.org/preview/pandas-dev/pandas/1234/
Any feedback or problem please let me know. | https://api.github.com/repos/pandas-dev/pandas/pulls/57112 | 2024-01-28T08:27:26Z | 2024-01-31T17:43:26Z | 2024-01-31T17:43:26Z | 2024-02-01T05:08:16Z |
DEPR: Enforce deprecation of removing axis from all groupby ops | diff --git a/ci/code_checks.sh b/ci/code_checks.sh
index f3023bea3743e..70d3efb80fd84 100755
--- a/ci/code_checks.sh
+++ b/ci/code_checks.sh
@@ -181,6 +181,7 @@ if [[ -z "$CHECK" || "$CHECK" == "docstrings" ]]; then
pandas.core.groupby.SeriesGroupBy.rolling\
pandas.core.groupby.DataFrameGroupBy.hist\
pandas.core.groupby.DataFrameGroupBy.plot\
+ pandas.core.groupby.DataFrameGroupBy.corrwith\
pandas.core.groupby.SeriesGroupBy.plot\
pandas.core.window.rolling.Rolling.quantile\
pandas.core.window.expanding.Expanding.quantile\
diff --git a/doc/source/reference/groupby.rst b/doc/source/reference/groupby.rst
index 771163ae1b0bc..3b02ffe20c10e 100644
--- a/doc/source/reference/groupby.rst
+++ b/doc/source/reference/groupby.rst
@@ -80,7 +80,6 @@ Function application
DataFrameGroupBy.describe
DataFrameGroupBy.diff
DataFrameGroupBy.ffill
- DataFrameGroupBy.fillna
DataFrameGroupBy.first
DataFrameGroupBy.head
DataFrameGroupBy.idxmax
@@ -131,7 +130,6 @@ Function application
SeriesGroupBy.describe
SeriesGroupBy.diff
SeriesGroupBy.ffill
- SeriesGroupBy.fillna
SeriesGroupBy.first
SeriesGroupBy.head
SeriesGroupBy.last
diff --git a/doc/source/user_guide/groupby.rst b/doc/source/user_guide/groupby.rst
index 11863f8aead31..a25e2ed179b80 100644
--- a/doc/source/user_guide/groupby.rst
+++ b/doc/source/user_guide/groupby.rst
@@ -289,7 +289,7 @@ the number of groups, which is the same as the length of the ``groups`` dictiona
In [1]: gb.<TAB> # noqa: E225, E999
gb.agg gb.boxplot gb.cummin gb.describe gb.filter gb.get_group gb.height gb.last gb.median gb.ngroups gb.plot gb.rank gb.std gb.transform
gb.aggregate gb.count gb.cumprod gb.dtype gb.first gb.groups gb.hist gb.max gb.min gb.nth gb.prod gb.resample gb.sum gb.var
- gb.apply gb.cummax gb.cumsum gb.fillna gb.gender gb.head gb.indices gb.mean gb.name gb.ohlc gb.quantile gb.size gb.tail gb.weight
+ gb.apply gb.cummax gb.cumsum gb.gender gb.head gb.indices gb.mean gb.name gb.ohlc gb.quantile gb.size gb.tail gb.weight
.. _groupby.multiindex:
diff --git a/doc/source/whatsnew/v3.0.0.rst b/doc/source/whatsnew/v3.0.0.rst
index ebcfd16f92d29..4accf8be46b9e 100644
--- a/doc/source/whatsnew/v3.0.0.rst
+++ b/doc/source/whatsnew/v3.0.0.rst
@@ -101,6 +101,8 @@ Deprecations
Removal of prior version deprecations/changes
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+- Removed :meth:`DataFrameGroupby.fillna` and :meth:`SeriesGroupBy.fillna` (:issue:`55719`)
+- 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`)
.. ---------------------------------------------------------------------------
diff --git a/pandas/core/groupby/base.py b/pandas/core/groupby/base.py
index a443597347283..3f776cf75d43a 100644
--- a/pandas/core/groupby/base.py
+++ b/pandas/core/groupby/base.py
@@ -70,7 +70,6 @@ class OutputKey:
"cumsum",
"diff",
"ffill",
- "fillna",
"ngroup",
"pct_change",
"rank",
diff --git a/pandas/core/groupby/generic.py b/pandas/core/groupby/generic.py
index 9598bc0db02cc..351b4bff0162e 100644
--- a/pandas/core/groupby/generic.py
+++ b/pandas/core/groupby/generic.py
@@ -24,10 +24,7 @@
import numpy as np
-from pandas._libs import (
- Interval,
- lib,
-)
+from pandas._libs import Interval
from pandas._libs.hashtable import duplicated
from pandas.errors import SpecificationError
from pandas.util._decorators import (
@@ -93,16 +90,13 @@
if TYPE_CHECKING:
from collections.abc import (
Hashable,
- Mapping,
Sequence,
)
from pandas._typing import (
ArrayLike,
- Axis,
AxisInt,
CorrelationMethod,
- FillnaOptions,
IndexLabel,
Manager,
Manager2D,
@@ -878,108 +872,9 @@ def build_codes(lev_codes: np.ndarray) -> np.ndarray:
result = result.reset_index()
return result
- def fillna(
- self,
- value: object | ArrayLike | None = None,
- method: FillnaOptions | None = None,
- axis: Axis | None | lib.NoDefault = lib.no_default,
- inplace: bool = False,
- limit: int | None = None,
- downcast: dict | None | lib.NoDefault = lib.no_default,
- ) -> Series | None:
- """
- Fill NA/NaN values using the specified method within groups.
-
- .. deprecated:: 2.2.0
- This method is deprecated and will be removed in a future version.
- Use the :meth:`.SeriesGroupBy.ffill` or :meth:`.SeriesGroupBy.bfill`
- for forward or backward filling instead. If you want to fill with a
- single value, use :meth:`Series.fillna` instead.
-
- Parameters
- ----------
- value : scalar, dict, Series, or DataFrame
- Value to use to fill holes (e.g. 0), alternately a
- dict/Series/DataFrame of values specifying which value to use for
- each index (for a Series) or column (for a DataFrame). Values not
- in the dict/Series/DataFrame will not be filled. This value cannot
- be a list. Users wanting to use the ``value`` argument and not ``method``
- should prefer :meth:`.Series.fillna` as this
- will produce the same result and be more performant.
- method : {{'bfill', 'ffill', None}}, default None
- Method to use for filling holes. ``'ffill'`` will propagate
- the last valid observation forward within a group.
- ``'bfill'`` will use next valid observation to fill the gap.
- axis : {0 or 'index', 1 or 'columns'}
- Unused, only for compatibility with :meth:`DataFrameGroupBy.fillna`.
- inplace : bool, default False
- Broken. Do not set to True.
- limit : int, default None
- If method is specified, this is the maximum number of consecutive
- NaN values to forward/backward fill within a group. In other words,
- if there is a gap with more than this number of consecutive NaNs,
- it will only be partially filled. If method is not specified, this is the
- maximum number of entries along the entire axis where NaNs will be
- filled. Must be greater than 0 if not None.
- downcast : dict, default is None
- A dict of item->dtype of what to downcast if possible,
- or the string 'infer' which will try to downcast to an appropriate
- equal type (e.g. float64 to int64 if possible).
-
- Returns
- -------
- Series
- Object with missing values filled within groups.
-
- See Also
- --------
- ffill : Forward fill values within a group.
- bfill : Backward fill values within a group.
-
- Examples
- --------
- For SeriesGroupBy:
-
- >>> lst = ['cat', 'cat', 'cat', 'mouse', 'mouse']
- >>> ser = pd.Series([1, None, None, 2, None], index=lst)
- >>> ser
- cat 1.0
- cat NaN
- cat NaN
- mouse 2.0
- mouse NaN
- dtype: float64
- >>> ser.groupby(level=0).fillna(0, limit=1)
- cat 1.0
- cat 0.0
- cat NaN
- mouse 2.0
- mouse 0.0
- dtype: float64
- """
- warnings.warn(
- f"{type(self).__name__}.fillna is deprecated and "
- "will be removed in a future version. Use obj.ffill() or obj.bfill() "
- "for forward or backward filling instead. If you want to fill with a "
- f"single value, use {type(self.obj).__name__}.fillna instead",
- FutureWarning,
- stacklevel=find_stack_level(),
- )
- result = self._op_via_apply(
- "fillna",
- value=value,
- method=method,
- axis=axis,
- inplace=inplace,
- limit=limit,
- downcast=downcast,
- )
- return result
-
def take(
self,
indices: TakeIndexer,
- axis: Axis | lib.NoDefault = lib.no_default,
**kwargs,
) -> Series:
"""
@@ -997,14 +892,6 @@ def take(
----------
indices : array-like
An array of ints indicating which positions to take in each group.
- axis : {0 or 'index', 1 or 'columns', None}, default 0
- The axis on which to select elements. ``0`` means that we are
- selecting rows, ``1`` means that we are selecting columns.
- For `SeriesGroupBy` this parameter is unused and defaults to 0.
-
- .. deprecated:: 2.1.0
- For axis=1, operate on the underlying object instead. Otherwise
- the axis keyword is not necessary.
**kwargs
For compatibility with :meth:`numpy.take`. Has no effect on the
@@ -1060,12 +947,11 @@ def take(
1 monkey
Name: name, dtype: object
"""
- result = self._op_via_apply("take", indices=indices, axis=axis, **kwargs)
+ result = self._op_via_apply("take", indices=indices, axis=0, **kwargs)
return result
def skew(
self,
- axis: Axis | lib.NoDefault = lib.no_default,
skipna: bool = True,
numeric_only: bool = False,
**kwargs,
@@ -1077,14 +963,6 @@ def skew(
Parameters
----------
- axis : {0 or 'index', 1 or 'columns', None}, default 0
- Axis for the function to be applied on.
- This parameter is only for compatibility with DataFrame and is unused.
-
- .. deprecated:: 2.1.0
- For axis=1, operate on the underlying object instead. Otherwise
- the axis keyword is not necessary.
-
skipna : bool, default True
Exclude NA/null values when computing the result.
@@ -1126,18 +1004,6 @@ def skew(
Parrot 1.457863
Name: Max Speed, dtype: float64
"""
- if axis is lib.no_default:
- axis = 0
-
- if axis != 0:
- result = self._op_via_apply(
- "skew",
- axis=axis,
- skipna=skipna,
- numeric_only=numeric_only,
- **kwargs,
- )
- return result
def alt(obj):
# This should not be reached since the cython path should raise
@@ -1177,16 +1043,12 @@ def nsmallest(
return result
@doc(Series.idxmin.__doc__)
- def idxmin(
- self, axis: Axis | lib.NoDefault = lib.no_default, skipna: bool = True
- ) -> Series:
- return self._idxmax_idxmin("idxmin", axis=axis, skipna=skipna)
+ def idxmin(self, skipna: bool = True) -> Series:
+ return self._idxmax_idxmin("idxmin", skipna=skipna)
@doc(Series.idxmax.__doc__)
- def idxmax(
- self, axis: Axis | lib.NoDefault = lib.no_default, skipna: bool = True
- ) -> Series:
- return self._idxmax_idxmin("idxmax", axis=axis, skipna=skipna)
+ def idxmax(self, skipna: bool = True) -> Series:
+ return self._idxmax_idxmin("idxmax", skipna=skipna)
@doc(Series.corr.__doc__)
def corr(
@@ -2101,27 +1963,16 @@ def nunique(self, dropna: bool = True) -> DataFrame:
def idxmax(
self,
- axis: Axis | None | lib.NoDefault = lib.no_default,
skipna: bool = True,
numeric_only: bool = False,
) -> DataFrame:
"""
- Return index of first occurrence of maximum over requested axis.
+ Return index of first occurrence of maximum in each group.
NA/null values are excluded.
Parameters
----------
- axis : {{0 or 'index', 1 or 'columns'}}, default None
- The axis to use. 0 or 'index' for row-wise, 1 or 'columns' for column-wise.
- If axis is not provided, grouper's axis is used.
-
- .. versionchanged:: 2.0.0
-
- .. deprecated:: 2.1.0
- For axis=1, operate on the underlying object instead. Otherwise
- the axis keyword is not necessary.
-
skipna : bool, default True
Exclude NA/null values. If an entire row/column is NA, the result
will be NA.
@@ -2133,7 +1984,7 @@ def idxmax(
Returns
-------
Series
- Indexes of maxima along the specified axis.
+ Indexes of maxima in each group.
Raises
------
@@ -2168,42 +2019,21 @@ def idxmax(
consumption Wheat Products
co2_emissions Beef
dtype: object
-
- To return the index for the maximum value in each row, use ``axis="columns"``.
-
- >>> df.idxmax(axis="columns")
- Pork co2_emissions
- Wheat Products consumption
- Beef co2_emissions
- dtype: object
"""
- return self._idxmax_idxmin(
- "idxmax", axis=axis, numeric_only=numeric_only, skipna=skipna
- )
+ return self._idxmax_idxmin("idxmax", numeric_only=numeric_only, skipna=skipna)
def idxmin(
self,
- axis: Axis | None | lib.NoDefault = lib.no_default,
skipna: bool = True,
numeric_only: bool = False,
) -> DataFrame:
"""
- Return index of first occurrence of minimum over requested axis.
+ Return index of first occurrence of minimum in each group.
NA/null values are excluded.
Parameters
----------
- axis : {{0 or 'index', 1 or 'columns'}}, default None
- The axis to use. 0 or 'index' for row-wise, 1 or 'columns' for column-wise.
- If axis is not provided, grouper's axis is used.
-
- .. versionchanged:: 2.0.0
-
- .. deprecated:: 2.1.0
- For axis=1, operate on the underlying object instead. Otherwise
- the axis keyword is not necessary.
-
skipna : bool, default True
Exclude NA/null values. If an entire row/column is NA, the result
will be NA.
@@ -2215,7 +2045,7 @@ def idxmin(
Returns
-------
Series
- Indexes of minima along the specified axis.
+ Indexes of minima in each group.
Raises
------
@@ -2250,18 +2080,8 @@ def idxmin(
consumption Pork
co2_emissions Wheat Products
dtype: object
-
- To return the index for the minimum value in each row, use ``axis="columns"``.
-
- >>> df.idxmin(axis="columns")
- Pork consumption
- Wheat Products co2_emissions
- Beef consumption
- dtype: object
"""
- return self._idxmax_idxmin(
- "idxmin", axis=axis, numeric_only=numeric_only, skipna=skipna
- )
+ return self._idxmax_idxmin("idxmin", numeric_only=numeric_only, skipna=skipna)
boxplot = boxplot_frame_groupby
@@ -2378,156 +2198,9 @@ def value_counts(
"""
return self._value_counts(subset, normalize, sort, ascending, dropna)
- def fillna(
- self,
- value: Hashable | Mapping | Series | DataFrame | None = None,
- method: FillnaOptions | None = None,
- axis: Axis | None | lib.NoDefault = lib.no_default,
- inplace: bool = False,
- limit: int | None = None,
- downcast=lib.no_default,
- ) -> DataFrame | None:
- """
- Fill NA/NaN values using the specified method within groups.
-
- .. deprecated:: 2.2.0
- This method is deprecated and will be removed in a future version.
- Use the :meth:`.DataFrameGroupBy.ffill` or :meth:`.DataFrameGroupBy.bfill`
- for forward or backward filling instead. If you want to fill with a
- single value, use :meth:`DataFrame.fillna` instead.
-
- Parameters
- ----------
- value : scalar, dict, Series, or DataFrame
- Value to use to fill holes (e.g. 0), alternately a
- dict/Series/DataFrame of values specifying which value to use for
- each index (for a Series) or column (for a DataFrame). Values not
- in the dict/Series/DataFrame will not be filled. This value cannot
- be a list. Users wanting to use the ``value`` argument and not ``method``
- should prefer :meth:`.DataFrame.fillna` as this
- will produce the same result and be more performant.
- method : {{'bfill', 'ffill', None}}, default None
- Method to use for filling holes. ``'ffill'`` will propagate
- the last valid observation forward within a group.
- ``'bfill'`` will use next valid observation to fill the gap.
- axis : {0 or 'index', 1 or 'columns'}
- Axis along which to fill missing values. When the :class:`DataFrameGroupBy`
- ``axis`` argument is ``0``, using ``axis=1`` here will produce
- the same results as :meth:`.DataFrame.fillna`. When the
- :class:`DataFrameGroupBy` ``axis`` argument is ``1``, using ``axis=0``
- or ``axis=1`` here will produce the same results.
- inplace : bool, default False
- Broken. Do not set to True.
- limit : int, default None
- If method is specified, this is the maximum number of consecutive
- NaN values to forward/backward fill within a group. In other words,
- if there is a gap with more than this number of consecutive NaNs,
- it will only be partially filled. If method is not specified, this is the
- maximum number of entries along the entire axis where NaNs will be
- filled. Must be greater than 0 if not None.
- downcast : dict, default is None
- A dict of item->dtype of what to downcast if possible,
- or the string 'infer' which will try to downcast to an appropriate
- equal type (e.g. float64 to int64 if possible).
-
- Returns
- -------
- DataFrame
- Object with missing values filled.
-
- See Also
- --------
- ffill : Forward fill values within a group.
- bfill : Backward fill values within a group.
-
- Examples
- --------
- >>> df = pd.DataFrame(
- ... {
- ... "key": [0, 0, 1, 1, 1],
- ... "A": [np.nan, 2, np.nan, 3, np.nan],
- ... "B": [2, 3, np.nan, np.nan, np.nan],
- ... "C": [np.nan, np.nan, 2, np.nan, np.nan],
- ... }
- ... )
- >>> df
- key A B C
- 0 0 NaN 2.0 NaN
- 1 0 2.0 3.0 NaN
- 2 1 NaN NaN 2.0
- 3 1 3.0 NaN NaN
- 4 1 NaN NaN NaN
-
- Propagate non-null values forward or backward within each group along columns.
-
- >>> df.groupby("key").fillna(method="ffill")
- A B C
- 0 NaN 2.0 NaN
- 1 2.0 3.0 NaN
- 2 NaN NaN 2.0
- 3 3.0 NaN 2.0
- 4 3.0 NaN 2.0
-
- >>> df.groupby("key").fillna(method="bfill")
- A B C
- 0 2.0 2.0 NaN
- 1 2.0 3.0 NaN
- 2 3.0 NaN 2.0
- 3 3.0 NaN NaN
- 4 NaN NaN NaN
-
- Propagate non-null values forward or backward within each group along rows.
-
- >>> df.T.groupby(np.array([0, 0, 1, 1])).fillna(method="ffill").T
- key A B C
- 0 0.0 0.0 2.0 2.0
- 1 0.0 2.0 3.0 3.0
- 2 1.0 1.0 NaN 2.0
- 3 1.0 3.0 NaN NaN
- 4 1.0 1.0 NaN NaN
-
- >>> df.T.groupby(np.array([0, 0, 1, 1])).fillna(method="bfill").T
- key A B C
- 0 0.0 NaN 2.0 NaN
- 1 0.0 2.0 3.0 NaN
- 2 1.0 NaN 2.0 2.0
- 3 1.0 3.0 NaN NaN
- 4 1.0 NaN NaN NaN
-
- Only replace the first NaN element within a group along rows.
-
- >>> df.groupby("key").fillna(method="ffill", limit=1)
- A B C
- 0 NaN 2.0 NaN
- 1 2.0 3.0 NaN
- 2 NaN NaN 2.0
- 3 3.0 NaN 2.0
- 4 3.0 NaN NaN
- """
- warnings.warn(
- f"{type(self).__name__}.fillna is deprecated and "
- "will be removed in a future version. Use obj.ffill() or obj.bfill() "
- "for forward or backward filling instead. If you want to fill with a "
- f"single value, use {type(self.obj).__name__}.fillna instead",
- FutureWarning,
- stacklevel=find_stack_level(),
- )
-
- result = self._op_via_apply(
- "fillna",
- value=value,
- method=method,
- axis=axis,
- inplace=inplace,
- limit=limit,
- downcast=downcast,
- )
- return result
-
def take(
self,
indices: TakeIndexer,
- axis: Axis | None | lib.NoDefault = lib.no_default,
**kwargs,
) -> DataFrame:
"""
@@ -2545,13 +2218,6 @@ def take(
----------
indices : array-like
An array of ints indicating which positions to take.
- axis : {0 or 'index', 1 or 'columns', None}, default 0
- The axis on which to select elements. ``0`` means that we are
- selecting rows, ``1`` means that we are selecting columns.
-
- .. deprecated:: 2.1.0
- For axis=1, operate on the underlying object instead. Otherwise
- the axis keyword is not necessary.
**kwargs
For compatibility with :meth:`numpy.take`. Has no effect on the
@@ -2610,8 +2276,6 @@ def take(
2 1 monkey mammal NaN
2 lion mammal 80.5
- Take elements at indices 1 and 2 along the axis 1 (column selection).
-
We may take elements using negative integers for positive indices,
starting from the end of the object, just like with Python lists.
@@ -2622,12 +2286,11 @@ def take(
2 0 rabbit mammal 15.0
1 monkey mammal NaN
"""
- result = self._op_via_apply("take", indices=indices, axis=axis, **kwargs)
+ result = self._op_via_apply("take", indices=indices, axis=0, **kwargs)
return result
def skew(
self,
- axis: Axis | None | lib.NoDefault = lib.no_default,
skipna: bool = True,
numeric_only: bool = False,
**kwargs,
@@ -2639,17 +2302,6 @@ def skew(
Parameters
----------
- axis : {0 or 'index', 1 or 'columns', None}, default 0
- Axis for the function to be applied on.
-
- Specifying ``axis=None`` will apply the aggregation across both axes.
-
- .. versionadded:: 2.0.0
-
- .. deprecated:: 2.1.0
- For axis=1, operate on the underlying object instead. Otherwise
- the axis keyword is not necessary.
-
skipna : bool, default True
Exclude NA/null values when computing the result.
@@ -2699,18 +2351,6 @@ def skew(
bird NaN
mammal 1.669046
"""
- if axis is lib.no_default:
- axis = 0
-
- if axis != 0:
- result = self._op_via_apply(
- "skew",
- axis=axis,
- skipna=skipna,
- numeric_only=numeric_only,
- **kwargs,
- )
- return result
def alt(obj):
# This should not be reached since the cython path should raise
@@ -2812,7 +2452,6 @@ def dtypes(self) -> Series:
def corrwith(
self,
other: DataFrame | Series,
- axis: Axis | lib.NoDefault = lib.no_default,
drop: bool = False,
method: CorrelationMethod = "pearson",
numeric_only: bool = False,
@@ -2820,7 +2459,6 @@ def corrwith(
result = self._op_via_apply(
"corrwith",
other=other,
- axis=axis,
drop=drop,
method=method,
numeric_only=numeric_only,
diff --git a/pandas/core/groupby/groupby.py b/pandas/core/groupby/groupby.py
index 1ca2b4ff511ca..1f0e0567446c6 100644
--- a/pandas/core/groupby/groupby.py
+++ b/pandas/core/groupby/groupby.py
@@ -1399,25 +1399,10 @@ def _op_via_apply(self, name: str, *args, **kwargs):
f = getattr(type(self._obj_with_exclusions), name)
sig = inspect.signature(f)
- if "axis" in kwargs and kwargs["axis"] is not lib.no_default:
- axis = self.obj._get_axis_number(kwargs["axis"])
- self._deprecate_axis(axis, name)
- elif "axis" in kwargs:
- # exclude skew here because that was already defaulting to lib.no_default
- # before this deprecation was instituted
- if name == "skew":
- pass
- elif name == "fillna":
- # maintain the behavior from before the deprecation
- kwargs["axis"] = None
- else:
- kwargs["axis"] = 0
-
# a little trickery for aggregation functions that need an axis
# argument
if "axis" in sig.parameters:
- if kwargs.get("axis", None) is None or kwargs.get("axis") is lib.no_default:
- kwargs["axis"] = self.axis
+ kwargs["axis"] = self.axis
def curried(x):
return f(x, *args, **kwargs)
@@ -4721,7 +4706,6 @@ def rank(
ascending: bool = True,
na_option: str = "keep",
pct: bool = False,
- axis: AxisInt | lib.NoDefault = lib.no_default,
) -> NDFrameT:
"""
Provide the rank of values within each group.
@@ -4742,12 +4726,6 @@ def rank(
* bottom: smallest rank if descending.
pct : bool, default False
Compute percentage rank of data within each group.
- axis : int, default 0
- The axis of the object over which to compute the rank.
-
- .. deprecated:: 2.1.0
- For axis=1, operate on the underlying object instead. Otherwise
- the axis keyword is not necessary.
Returns
-------
@@ -4792,40 +4770,24 @@ def rank(
msg = "na_option must be one of 'keep', 'top', or 'bottom'"
raise ValueError(msg)
- if axis is not lib.no_default:
- axis = self.obj._get_axis_number(axis)
- self._deprecate_axis(axis, "rank")
- else:
- axis = 0
-
kwargs = {
"ties_method": method,
"ascending": ascending,
"na_option": na_option,
"pct": pct,
}
- if axis != 0:
- # DataFrame uses different keyword name
- kwargs["method"] = kwargs.pop("ties_method")
- f = lambda x: x.rank(axis=axis, numeric_only=False, **kwargs)
- result = self._python_apply_general(
- f, self._selected_obj, is_transform=True
- )
- return result
return self._cython_transform(
"rank",
numeric_only=False,
- axis=axis,
+ axis=0,
**kwargs,
)
@final
@Substitution(name="groupby")
@Substitution(see_also=_common_see_also)
- def cumprod(
- self, axis: Axis | lib.NoDefault = lib.no_default, *args, **kwargs
- ) -> NDFrameT:
+ def cumprod(self, *args, **kwargs) -> NDFrameT:
"""
Cumulative product for each group.
@@ -4869,24 +4831,12 @@ def cumprod(
bull 6 9
"""
nv.validate_groupby_func("cumprod", args, kwargs, ["numeric_only", "skipna"])
- if axis is not lib.no_default:
- axis = self.obj._get_axis_number(axis)
- self._deprecate_axis(axis, "cumprod")
- else:
- axis = 0
-
- if axis != 0:
- f = lambda x: x.cumprod(axis=axis, **kwargs)
- return self._python_apply_general(f, self._selected_obj, is_transform=True)
-
return self._cython_transform("cumprod", **kwargs)
@final
@Substitution(name="groupby")
@Substitution(see_also=_common_see_also)
- def cumsum(
- self, axis: Axis | lib.NoDefault = lib.no_default, *args, **kwargs
- ) -> NDFrameT:
+ def cumsum(self, *args, **kwargs) -> NDFrameT:
"""
Cumulative sum for each group.
@@ -4930,16 +4880,6 @@ def cumsum(
lion 6 9
"""
nv.validate_groupby_func("cumsum", args, kwargs, ["numeric_only", "skipna"])
- if axis is not lib.no_default:
- axis = self.obj._get_axis_number(axis)
- self._deprecate_axis(axis, "cumsum")
- else:
- axis = 0
-
- if axis != 0:
- f = lambda x: x.cumsum(axis=axis, **kwargs)
- return self._python_apply_general(f, self._selected_obj, is_transform=True)
-
return self._cython_transform("cumsum", **kwargs)
@final
@@ -4947,7 +4887,6 @@ def cumsum(
@Substitution(see_also=_common_see_also)
def cummin(
self,
- axis: AxisInt | lib.NoDefault = lib.no_default,
numeric_only: bool = False,
**kwargs,
) -> NDFrameT:
@@ -5000,19 +4939,6 @@ def cummin(
turtle 6 9
"""
skipna = kwargs.get("skipna", True)
- if axis is not lib.no_default:
- axis = self.obj._get_axis_number(axis)
- self._deprecate_axis(axis, "cummin")
- else:
- axis = 0
-
- if axis != 0:
- f = lambda x: np.minimum.accumulate(x, axis)
- obj = self._selected_obj
- if numeric_only:
- obj = obj._get_numeric_data()
- return self._python_apply_general(f, obj, is_transform=True)
-
return self._cython_transform(
"cummin", numeric_only=numeric_only, skipna=skipna
)
@@ -5022,7 +4948,6 @@ def cummin(
@Substitution(see_also=_common_see_also)
def cummax(
self,
- axis: AxisInt | lib.NoDefault = lib.no_default,
numeric_only: bool = False,
**kwargs,
) -> NDFrameT:
@@ -5075,19 +5000,6 @@ def cummax(
bull 6 9
"""
skipna = kwargs.get("skipna", True)
- if axis is not lib.no_default:
- axis = self.obj._get_axis_number(axis)
- self._deprecate_axis(axis, "cummax")
- else:
- axis = 0
-
- if axis != 0:
- f = lambda x: np.maximum.accumulate(x, axis)
- obj = self._selected_obj
- if numeric_only:
- obj = obj._get_numeric_data()
- return self._python_apply_general(f, obj, is_transform=True)
-
return self._cython_transform(
"cummax", numeric_only=numeric_only, skipna=skipna
)
@@ -5098,7 +5010,6 @@ def shift(
self,
periods: int | Sequence[int] = 1,
freq=None,
- axis: Axis | lib.NoDefault = lib.no_default,
fill_value=lib.no_default,
suffix: str | None = None,
):
@@ -5114,13 +5025,6 @@ def shift(
each period.
freq : str, optional
Frequency string.
- axis : axis to shift, default 0
- Shift direction.
-
- .. deprecated:: 2.1.0
- For axis=1, operate on the underlying object instead. Otherwise
- the axis keyword is not necessary.
-
fill_value : optional
The scalar value to use for newly introduced missing values.
@@ -5178,17 +5082,7 @@ def shift(
catfish NaN NaN
goldfish 5.0 8.0
"""
- if axis is not lib.no_default:
- axis = self.obj._get_axis_number(axis)
- self._deprecate_axis(axis, "shift")
- else:
- axis = 0
-
if is_list_like(periods):
- if axis == 1:
- raise ValueError(
- "If `periods` contains multiple shifts, `axis` cannot be 1."
- )
periods = cast(Sequence, periods)
if len(periods) == 0:
raise ValueError("If `periods` is an iterable, it cannot be empty.")
@@ -5212,11 +5106,11 @@ def shift(
f"Periods must be integer, but {period} is {type(period)}."
)
period = cast(int, period)
- if freq is not None or axis != 0:
+ if freq is not None:
f = lambda x: x.shift(
period, # pylint: disable=cell-var-from-loop
freq,
- axis,
+ 0, # axis
fill_value,
)
shifted = self._python_apply_general(
@@ -5256,7 +5150,8 @@ def shift(
@Substitution(name="groupby")
@Substitution(see_also=_common_see_also)
def diff(
- self, periods: int = 1, axis: AxisInt | lib.NoDefault = lib.no_default
+ self,
+ periods: int = 1,
) -> NDFrameT:
"""
First discrete difference of element.
@@ -5268,12 +5163,6 @@ def diff(
----------
periods : int, default 1
Periods to shift for calculating difference, accepts negative values.
- axis : axis to shift, default 0
- Take difference over rows (0) or columns (1).
-
- .. deprecated:: 2.1.0
- For axis=1, operate on the underlying object instead. Otherwise
- the axis keyword is not necessary.
Returns
-------
@@ -5327,15 +5216,6 @@ def diff(
mouse 1.0 -2.0
mouse -5.0 -1.0
"""
- if axis is not lib.no_default:
- axis = self.obj._get_axis_number(axis)
- self._deprecate_axis(axis, "diff")
- else:
- axis = 0
-
- if axis != 0:
- return self.apply(lambda x: x.diff(periods=periods, axis=axis))
-
obj = self._obj_with_exclusions
shifted = self.shift(periods=periods)
@@ -5361,7 +5241,6 @@ def pct_change(
fill_method: FillnaOptions | None | lib.NoDefault = lib.no_default,
limit: int | None | lib.NoDefault = lib.no_default,
freq=None,
- axis: Axis | lib.NoDefault = lib.no_default,
):
"""
Calculate pct_change of each value to previous entry in group.
@@ -5437,21 +5316,15 @@ def pct_change(
if limit is lib.no_default:
limit = None
- if axis is not lib.no_default:
- axis = self.obj._get_axis_number(axis)
- self._deprecate_axis(axis, "pct_change")
- else:
- axis = 0
-
# TODO(GH#23918): Remove this conditional for SeriesGroupBy when
# GH#23918 is fixed
- if freq is not None or axis != 0:
+ if freq is not None:
f = lambda x: x.pct_change(
periods=periods,
fill_method=fill_method,
limit=limit,
freq=freq,
- axis=axis,
+ axis=0,
)
return self._python_apply_general(f, self._selected_obj, is_transform=True)
@@ -5803,7 +5676,6 @@ def _idxmax_idxmin(
self,
how: Literal["idxmax", "idxmin"],
ignore_unobserved: bool = False,
- axis: Axis | None | lib.NoDefault = lib.no_default,
skipna: bool = True,
numeric_only: bool = False,
) -> NDFrameT:
@@ -5813,9 +5685,6 @@ def _idxmax_idxmin(
----------
how : {'idxmin', 'idxmax'}
Whether to compute idxmin or idxmax.
- axis : {{0 or 'index', 1 or 'columns'}}, default None
- The axis to use. 0 or 'index' for row-wise, 1 or 'columns' for column-wise.
- If axis is not provided, grouper's axis is used.
numeric_only : bool, default False
Include only float, int, boolean columns.
skipna : bool, default True
@@ -5830,14 +5699,6 @@ def _idxmax_idxmin(
Series or DataFrame
idxmax or idxmin for the groupby operation.
"""
- if axis is not lib.no_default:
- if axis is None:
- axis = self.axis
- axis = self.obj._get_axis_number(axis)
- self._deprecate_axis(axis, how)
- else:
- axis = self.axis
-
if not self.observed and any(
ping._passed_categorical for ping in self._grouper.groupings
):
@@ -5877,27 +5738,6 @@ def _idxmax_idxmin(
stacklevel=find_stack_level(),
)
- if axis == 1:
- try:
-
- def func(df):
- method = getattr(df, how)
- return method(axis=axis, skipna=skipna, numeric_only=numeric_only)
-
- func.__name__ = how
- result = self._python_apply_general(
- func, self._obj_with_exclusions, not_indexed_same=True
- )
- except ValueError as err:
- name = "argmax" if how == "idxmax" else "argmin"
- if f"attempt to get {name} of an empty sequence" in str(err):
- raise ValueError(
- f"Can't get {how} of an empty group due to unobserved "
- "categories. Specify observed=True in groupby instead."
- ) from None
- raise
- return result
-
result = self._agg_general(
numeric_only=numeric_only,
min_count=1,
diff --git a/pandas/tests/groupby/aggregate/test_aggregate.py b/pandas/tests/groupby/aggregate/test_aggregate.py
index 86f03b04fddb3..0e86f95a93091 100644
--- a/pandas/tests/groupby/aggregate/test_aggregate.py
+++ b/pandas/tests/groupby/aggregate/test_aggregate.py
@@ -263,18 +263,9 @@ def test_std_masked_dtype(any_numeric_ea_dtype):
def test_agg_str_with_kwarg_axis_1_raises(df, reduction_func):
gb = df.groupby(level=0)
- warn_msg = f"DataFrameGroupBy.{reduction_func} with axis=1 is deprecated"
- if reduction_func in ("idxmax", "idxmin"):
- error = TypeError
- msg = "'[<>]' not supported between instances of 'float' and 'str'"
- warn = FutureWarning
- else:
- error = ValueError
- msg = f"Operation {reduction_func} does not support axis=1"
- warn = None
- with pytest.raises(error, match=msg):
- with tm.assert_produces_warning(warn, match=warn_msg):
- gb.agg(reduction_func, axis=1)
+ msg = f"Operation {reduction_func} does not support axis=1"
+ with pytest.raises(ValueError, match=msg):
+ gb.agg(reduction_func, axis=1)
@pytest.mark.parametrize(
diff --git a/pandas/tests/groupby/methods/test_corrwith.py b/pandas/tests/groupby/methods/test_corrwith.py
deleted file mode 100644
index 53e8bdc4534dc..0000000000000
--- a/pandas/tests/groupby/methods/test_corrwith.py
+++ /dev/null
@@ -1,24 +0,0 @@
-import numpy as np
-
-from pandas import (
- DataFrame,
- Index,
- Series,
-)
-import pandas._testing as tm
-
-
-def test_corrwith_with_1_axis():
- # GH 47723
- df = DataFrame({"a": [1, 1, 2], "b": [3, 7, 4]})
- gb = df.groupby("a")
-
- msg = "DataFrameGroupBy.corrwith with axis=1 is deprecated"
- with tm.assert_produces_warning(FutureWarning, match=msg):
- result = gb.corrwith(df, axis=1)
- index = Index(
- data=[(1, 0), (1, 1), (1, 2), (2, 2), (2, 0), (2, 1)],
- name=("a", None),
- )
- expected = Series([np.nan] * 6, index=index)
- tm.assert_series_equal(result, expected)
diff --git a/pandas/tests/groupby/methods/test_rank.py b/pandas/tests/groupby/methods/test_rank.py
index 18869033d05c6..1fb878cbc1111 100644
--- a/pandas/tests/groupby/methods/test_rank.py
+++ b/pandas/tests/groupby/methods/test_rank.py
@@ -605,80 +605,6 @@ def test_rank_pct_equal_values_on_group_transition(use_nan):
tm.assert_series_equal(result, expected)
-def test_rank_multiindex():
- # GH27721
- df = concat(
- {
- "a": DataFrame({"col1": [3, 4], "col2": [1, 2]}),
- "b": DataFrame({"col3": [5, 6], "col4": [7, 8]}),
- },
- axis=1,
- )
-
- msg = "DataFrame.groupby with axis=1 is deprecated"
- with tm.assert_produces_warning(FutureWarning, match=msg):
- gb = df.groupby(level=0, axis=1)
- msg = "DataFrameGroupBy.rank with axis=1 is deprecated"
- with tm.assert_produces_warning(FutureWarning, match=msg):
- result = gb.rank(axis=1)
-
- expected = concat(
- [
- df["a"].rank(axis=1),
- df["b"].rank(axis=1),
- ],
- axis=1,
- keys=["a", "b"],
- )
- tm.assert_frame_equal(result, expected)
-
-
-def test_groupby_axis0_rank_axis1():
- # GH#41320
- df = DataFrame(
- {0: [1, 3, 5, 7], 1: [2, 4, 6, 8], 2: [1.5, 3.5, 5.5, 7.5]},
- index=["a", "a", "b", "b"],
- )
- msg = "The 'axis' keyword in DataFrame.groupby is deprecated"
- with tm.assert_produces_warning(FutureWarning, match=msg):
- gb = df.groupby(level=0, axis=0)
-
- msg = "DataFrameGroupBy.rank with axis=1 is deprecated"
- with tm.assert_produces_warning(FutureWarning, match=msg):
- res = gb.rank(axis=1)
-
- # This should match what we get when "manually" operating group-by-group
- expected = concat([df.loc["a"].rank(axis=1), df.loc["b"].rank(axis=1)], axis=0)
- tm.assert_frame_equal(res, expected)
-
- # check that we haven't accidentally written a case that coincidentally
- # matches rank(axis=0)
- msg = "The 'axis' keyword in DataFrameGroupBy.rank"
- with tm.assert_produces_warning(FutureWarning, match=msg):
- alt = gb.rank(axis=0)
- assert not alt.equals(expected)
-
-
-def test_groupby_axis0_cummax_axis1():
- # case where groupby axis is 0 and axis keyword in transform is 1
-
- # df has mixed dtype -> multiple blocks
- df = DataFrame(
- {0: [1, 3, 5, 7], 1: [2, 4, 6, 8], 2: [1.5, 3.5, 5.5, 7.5]},
- index=["a", "a", "b", "b"],
- )
- msg = "The 'axis' keyword in DataFrame.groupby is deprecated"
- with tm.assert_produces_warning(FutureWarning, match=msg):
- gb = df.groupby(level=0, axis=0)
-
- msg = "DataFrameGroupBy.cummax with axis=1 is deprecated"
- with tm.assert_produces_warning(FutureWarning, match=msg):
- cmax = gb.cummax(axis=1)
- expected = df[[0, 1]].astype(np.float64)
- expected[2] = expected[1]
- tm.assert_frame_equal(cmax, expected)
-
-
def test_non_unique_index():
# GH 16577
df = DataFrame(
diff --git a/pandas/tests/groupby/test_api.py b/pandas/tests/groupby/test_api.py
index 5c5982954de2f..24a02bb6b2b91 100644
--- a/pandas/tests/groupby/test_api.py
+++ b/pandas/tests/groupby/test_api.py
@@ -68,7 +68,6 @@ def test_tab_completion(multiindex_dataframe_random_data):
"tail",
"resample",
"cummin",
- "fillna",
"cumsum",
"cumcount",
"ngroup",
@@ -191,11 +190,12 @@ def test_frame_consistency(groupby_func):
exclude_expected = {"skipna"}
elif groupby_func in ("pct_change",):
exclude_expected = {"kwargs"}
- exclude_result = {"axis"}
elif groupby_func in ("rank",):
exclude_expected = {"numeric_only"}
elif groupby_func in ("quantile",):
exclude_expected = {"method", "axis"}
+ if groupby_func not in ["pct_change", "size"]:
+ exclude_expected |= {"axis"}
# Ensure excluded arguments are actually in the signatures
assert result & exclude_result == exclude_result
@@ -229,8 +229,6 @@ def test_series_consistency(request, groupby_func):
exclude_expected, exclude_result = set(), set()
if groupby_func in ("any", "all"):
exclude_expected = {"kwargs", "bool_only", "axis"}
- elif groupby_func in ("diff",):
- exclude_result = {"axis"}
elif groupby_func in ("max", "min"):
exclude_expected = {"axis", "kwargs", "skipna"}
exclude_result = {"min_count", "engine", "engine_kwargs"}
@@ -248,13 +246,21 @@ def test_series_consistency(request, groupby_func):
exclude_expected = {"skipna"}
elif groupby_func in ("pct_change",):
exclude_expected = {"kwargs"}
- exclude_result = {"axis"}
elif groupby_func in ("rank",):
exclude_expected = {"numeric_only"}
elif groupby_func in ("idxmin", "idxmax"):
exclude_expected = {"args", "kwargs"}
elif groupby_func in ("quantile",):
exclude_result = {"numeric_only"}
+ if groupby_func not in [
+ "diff",
+ "pct_change",
+ "count",
+ "nunique",
+ "quantile",
+ "size",
+ ]:
+ exclude_expected |= {"axis"}
# Ensure excluded arguments are actually in the signatures
assert result & exclude_result == exclude_result
diff --git a/pandas/tests/groupby/test_cumulative.py b/pandas/tests/groupby/test_cumulative.py
index 1bdbef6d50c4c..28dcb38d173f2 100644
--- a/pandas/tests/groupby/test_cumulative.py
+++ b/pandas/tests/groupby/test_cumulative.py
@@ -304,16 +304,3 @@ def test_cython_api2():
# GH 5755 - cumsum is a transformer and should ignore as_index
result = df.groupby("A", as_index=False).cumsum()
tm.assert_frame_equal(result, expected)
-
- # GH 13994
- msg = "DataFrameGroupBy.cumsum with axis=1 is deprecated"
- with tm.assert_produces_warning(FutureWarning, match=msg):
- result = df.groupby("A").cumsum(axis=1)
- expected = df.cumsum(axis=1)
- tm.assert_frame_equal(result, expected)
-
- msg = "DataFrameGroupBy.cumprod with axis=1 is deprecated"
- with tm.assert_produces_warning(FutureWarning, match=msg):
- result = df.groupby("A").cumprod(axis=1)
- expected = df.cumprod(axis=1)
- tm.assert_frame_equal(result, expected)
diff --git a/pandas/tests/groupby/test_groupby.py b/pandas/tests/groupby/test_groupby.py
index 7f7d9544c3891..399cebb0d3706 100644
--- a/pandas/tests/groupby/test_groupby.py
+++ b/pandas/tests/groupby/test_groupby.py
@@ -2906,9 +2906,6 @@ def test_groupby_selection_other_methods(df):
g_exp = df[["C"]].groupby(df["A"])
# methods which aren't just .foo()
- warn_msg = "DataFrameGroupBy.fillna is deprecated"
- with tm.assert_produces_warning(FutureWarning, match=warn_msg):
- tm.assert_frame_equal(g.fillna(0), g_exp.fillna(0))
msg = "DataFrameGroupBy.dtypes is deprecated"
with tm.assert_produces_warning(FutureWarning, match=msg):
tm.assert_frame_equal(g.dtypes, g_exp.dtypes)
diff --git a/pandas/tests/groupby/test_missing.py b/pandas/tests/groupby/test_missing.py
index 6c029c10817d9..2b590c50371e9 100644
--- a/pandas/tests/groupby/test_missing.py
+++ b/pandas/tests/groupby/test_missing.py
@@ -5,7 +5,6 @@
from pandas import (
DataFrame,
Index,
- date_range,
)
import pandas._testing as tm
@@ -36,78 +35,6 @@ def test_groupby_fill_duplicate_column_names(func):
tm.assert_frame_equal(result, expected)
-def test_ffill_missing_arguments():
- # GH 14955
- df = DataFrame({"a": [1, 2], "b": [1, 1]})
- msg = "DataFrameGroupBy.fillna is deprecated"
- with tm.assert_produces_warning(FutureWarning, match=msg):
- with pytest.raises(ValueError, match="Must specify a fill"):
- df.groupby("b").fillna()
-
-
-@pytest.mark.parametrize(
- "method, expected", [("ffill", [None, "a", "a"]), ("bfill", ["a", "a", None])]
-)
-def test_fillna_with_string_dtype(method, expected):
- # GH 40250
- df = DataFrame({"a": pd.array([None, "a", None], dtype="string"), "b": [0, 0, 0]})
- grp = df.groupby("b")
- msg = "DataFrameGroupBy.fillna is deprecated"
- with tm.assert_produces_warning(FutureWarning, match=msg):
- result = grp.fillna(method=method)
- expected = DataFrame({"a": pd.array(expected, dtype="string")})
- tm.assert_frame_equal(result, expected)
-
-
-def test_fill_consistency():
- # GH9221
- # pass thru keyword arguments to the generated wrapper
- # are set if the passed kw is None (only)
- df = DataFrame(
- index=pd.MultiIndex.from_product(
- [["value1", "value2"], date_range("2014-01-01", "2014-01-06")]
- ),
- columns=Index(["1", "2"], name="id"),
- )
- df["1"] = [
- np.nan,
- 1,
- np.nan,
- np.nan,
- 11,
- np.nan,
- np.nan,
- 2,
- np.nan,
- np.nan,
- 22,
- np.nan,
- ]
- df["2"] = [
- np.nan,
- 3,
- np.nan,
- np.nan,
- 33,
- np.nan,
- np.nan,
- 4,
- np.nan,
- np.nan,
- 44,
- np.nan,
- ]
-
- msg = "The 'axis' keyword in DataFrame.groupby is deprecated"
- with tm.assert_produces_warning(FutureWarning, match=msg):
- expected = df.groupby(level=0, axis=0).fillna(method="ffill")
-
- msg = "DataFrame.groupby with axis=1 is deprecated"
- with tm.assert_produces_warning(FutureWarning, match=msg):
- result = df.T.groupby(level=0, axis=1).fillna(method="ffill").T
- tm.assert_frame_equal(result, expected)
-
-
@pytest.mark.parametrize("method", ["ffill", "bfill"])
@pytest.mark.parametrize("has_nan_group", [True, False])
def test_ffill_handles_nan_groups(dropna, method, has_nan_group):
diff --git a/pandas/tests/groupby/test_numeric_only.py b/pandas/tests/groupby/test_numeric_only.py
index ff4685b1e412d..1b435fd55d05e 100644
--- a/pandas/tests/groupby/test_numeric_only.py
+++ b/pandas/tests/groupby/test_numeric_only.py
@@ -1,6 +1,5 @@
import re
-import numpy as np
import pytest
from pandas._libs import lib
@@ -207,94 +206,6 @@ def _check(self, df, method, expected_columns, expected_columns_numeric):
tm.assert_index_equal(result.columns, expected_columns)
-@pytest.mark.parametrize("numeric_only", [True, False, None])
-def test_axis1_numeric_only(request, groupby_func, numeric_only, using_infer_string):
- if groupby_func in ("idxmax", "idxmin"):
- pytest.skip("idxmax and idx_min tested in test_idxmin_idxmax_axis1")
- if groupby_func in ("corrwith", "skew"):
- msg = "GH#47723 groupby.corrwith and skew do not correctly implement axis=1"
- request.applymarker(pytest.mark.xfail(reason=msg))
-
- df = DataFrame(
- np.random.default_rng(2).standard_normal((10, 4)), columns=["A", "B", "C", "D"]
- )
- df["E"] = "x"
- groups = [1, 2, 3, 1, 2, 3, 1, 2, 3, 4]
- gb = df.groupby(groups)
- method = getattr(gb, groupby_func)
- args = get_groupby_method_args(groupby_func, df)
- kwargs = {"axis": 1}
- if numeric_only is not None:
- # when numeric_only is None we don't pass any argument
- kwargs["numeric_only"] = numeric_only
-
- # Functions without numeric_only and axis args
- no_args = ("cumprod", "cumsum", "diff", "fillna", "pct_change", "rank", "shift")
- # Functions with axis args
- has_axis = (
- "cumprod",
- "cumsum",
- "diff",
- "pct_change",
- "rank",
- "shift",
- "cummax",
- "cummin",
- "idxmin",
- "idxmax",
- "fillna",
- )
- warn_msg = f"DataFrameGroupBy.{groupby_func} with axis=1 is deprecated"
- if numeric_only is not None and groupby_func in no_args:
- msg = "got an unexpected keyword argument 'numeric_only'"
- if groupby_func in ["cumprod", "cumsum"]:
- with pytest.raises(TypeError, match=msg):
- with tm.assert_produces_warning(FutureWarning, match=warn_msg):
- method(*args, **kwargs)
- else:
- with pytest.raises(TypeError, match=msg):
- method(*args, **kwargs)
- elif groupby_func not in has_axis:
- msg = "got an unexpected keyword argument 'axis'"
- with pytest.raises(TypeError, match=msg):
- method(*args, **kwargs)
- # fillna and shift are successful even on object dtypes
- elif (numeric_only is None or not numeric_only) and groupby_func not in (
- "fillna",
- "shift",
- ):
- msgs = (
- # cummax, cummin, rank
- "not supported between instances of",
- # cumprod
- "can't multiply sequence by non-int of type 'float'",
- # cumsum, diff, pct_change
- "unsupported operand type",
- "has no kernel",
- )
- if using_infer_string:
- import pyarrow as pa
-
- errs = (TypeError, pa.lib.ArrowNotImplementedError)
- else:
- errs = TypeError
- with pytest.raises(errs, match=f"({'|'.join(msgs)})"):
- with tm.assert_produces_warning(FutureWarning, match=warn_msg):
- method(*args, **kwargs)
- else:
- with tm.assert_produces_warning(FutureWarning, match=warn_msg):
- result = method(*args, **kwargs)
-
- df_expected = df.drop(columns="E").T if numeric_only else df.T
- expected = getattr(df_expected, groupby_func)(*args).T
- if groupby_func == "shift" and not numeric_only:
- # shift with axis=1 leaves the leftmost column as numeric
- # but transposing for expected gives us object dtype
- expected = expected.astype(float)
-
- tm.assert_equal(result, expected)
-
-
@pytest.mark.parametrize(
"kernel, has_arg",
[
@@ -310,7 +221,6 @@ def test_axis1_numeric_only(request, groupby_func, numeric_only, using_infer_str
("cumsum", True),
("diff", False),
("ffill", False),
- ("fillna", False),
("first", True),
("idxmax", True),
("idxmin", True),
diff --git a/pandas/tests/groupby/test_raises.py b/pandas/tests/groupby/test_raises.py
index 738711019b5fd..ee5c61794e96d 100644
--- a/pandas/tests/groupby/test_raises.py
+++ b/pandas/tests/groupby/test_raises.py
@@ -698,13 +698,3 @@ def test_groupby_raises_category_on_category(
else:
warn_msg = ""
_call_and_check(klass, msg, how, gb, groupby_func, args, warn_msg)
-
-
-def test_subsetting_columns_axis_1_raises():
- # GH 35443
- df = DataFrame({"a": [1], "b": [2], "c": [3]})
- msg = "DataFrame.groupby with axis=1 is deprecated"
- with tm.assert_produces_warning(FutureWarning, match=msg):
- gb = df.groupby("a", axis=1)
- with pytest.raises(ValueError, match="Cannot subset columns when using axis=1"):
- gb["b"]
diff --git a/pandas/tests/groupby/test_reductions.py b/pandas/tests/groupby/test_reductions.py
index bd188c729846c..08ce41edfb784 100644
--- a/pandas/tests/groupby/test_reductions.py
+++ b/pandas/tests/groupby/test_reductions.py
@@ -420,32 +420,6 @@ def test_first_last_skipna(any_real_nullable_dtype, sort, skipna, how):
tm.assert_frame_equal(result, expected)
-def test_idxmin_idxmax_axis1():
- df = DataFrame(
- np.random.default_rng(2).standard_normal((10, 4)), columns=["A", "B", "C", "D"]
- )
- df["A"] = [1, 2, 3, 1, 2, 3, 1, 2, 3, 4]
-
- gb = df.groupby("A")
-
- warn_msg = "DataFrameGroupBy.idxmax with axis=1 is deprecated"
- with tm.assert_produces_warning(FutureWarning, match=warn_msg):
- res = gb.idxmax(axis=1)
-
- alt = df.iloc[:, 1:].idxmax(axis=1)
- indexer = res.index.get_level_values(1)
-
- tm.assert_series_equal(alt[indexer], res.droplevel("A"))
-
- df["E"] = date_range("2016-01-01", periods=10)
- gb2 = df.groupby("A")
-
- msg = "'>' not supported between instances of 'Timestamp' and 'float'"
- with pytest.raises(TypeError, match=msg):
- with tm.assert_produces_warning(FutureWarning, match=warn_msg):
- gb2.idxmax(axis=1)
-
-
def test_groupby_mean_no_overflow():
# Regression test for (#22487)
df = DataFrame(
diff --git a/pandas/tests/groupby/transform/test_transform.py b/pandas/tests/groupby/transform/test_transform.py
index 40732b32f0111..3bccacf3dec6f 100644
--- a/pandas/tests/groupby/transform/test_transform.py
+++ b/pandas/tests/groupby/transform/test_transform.py
@@ -398,33 +398,12 @@ def test_dispatch_transform(tsframe):
grouped = df.groupby(lambda x: x.month)
- msg = "DataFrameGroupBy.fillna is deprecated"
- with tm.assert_produces_warning(FutureWarning, match=msg):
- filled = grouped.fillna(method="pad")
- msg = "Series.fillna with 'method' is deprecated"
- fillit = lambda x: x.fillna(method="pad")
- with tm.assert_produces_warning(FutureWarning, match=msg):
- expected = df.groupby(lambda x: x.month).transform(fillit)
+ filled = grouped.ffill()
+ fillit = lambda x: x.ffill()
+ expected = df.groupby(lambda x: x.month).transform(fillit)
tm.assert_frame_equal(filled, expected)
-def test_transform_fillna_null():
- df = DataFrame(
- {
- "price": [10, 10, 20, 20, 30, 30],
- "color": [10, 10, 20, 20, 30, 30],
- "cost": (100, 200, 300, 400, 500, 600),
- }
- )
- msg = "DataFrameGroupBy.fillna is deprecated"
- with tm.assert_produces_warning(FutureWarning, match=msg):
- with pytest.raises(ValueError, match="Must specify a fill 'value' or 'method'"):
- df.groupby(["price"]).transform("fillna")
- with tm.assert_produces_warning(FutureWarning, match=msg):
- with pytest.raises(ValueError, match="Must specify a fill 'value' or 'method'"):
- df.groupby(["price"]).fillna()
-
-
def test_transform_transformation_func(transformation_func):
# GH 30918
df = DataFrame(
@@ -1690,11 +1669,10 @@ def test_idxmin_idxmax_transform_args(how, skipna, numeric_only):
# GH#55268 - ensure *args are passed through when calling transform
df = DataFrame({"a": [1, 1, 1, 2], "b": [3.0, 4.0, np.nan, 6.0], "c": list("abcd")})
gb = df.groupby("a")
- msg = f"'axis' keyword in DataFrameGroupBy.{how} is deprecated"
- with tm.assert_produces_warning(FutureWarning, match=msg):
- result = gb.transform(how, 0, skipna, numeric_only)
warn = None if skipna else FutureWarning
msg = f"The behavior of DataFrameGroupBy.{how} with .* any-NA and skipna=False"
+ with tm.assert_produces_warning(warn, match=msg):
+ result = gb.transform(how, skipna, numeric_only)
with tm.assert_produces_warning(warn, match=msg):
expected = gb.transform(how, skipna=skipna, numeric_only=numeric_only)
tm.assert_frame_equal(result, expected)
| - [ ] closes #xxxx (Replace xxxx with the GitHub issue number)
- [ ] [Tests added and passed](https://pandas.pydata.org/pandas-docs/dev/development/contributing_codebase.html#writing-tests) if fixing a bug or adding a new feature
- [ ] All [code checks passed](https://pandas.pydata.org/pandas-docs/dev/development/contributing_codebase.html#pre-commit).
- [ ] Added [type annotations](https://pandas.pydata.org/pandas-docs/dev/development/contributing_codebase.html#type-hints) to new arguments/methods/functions.
- [ ] Added an entry in the latest `doc/source/whatsnew/vX.X.X.rst` file if fixing a bug or adding a new feature.
| https://api.github.com/repos/pandas-dev/pandas/pulls/57109 | 2024-01-28T02:50:41Z | 2024-01-31T18:39:06Z | 2024-01-31T18:39:06Z | 2024-01-31T21:29:45Z |
TYP: misc return values | diff --git a/pandas/_config/config.py b/pandas/_config/config.py
index b74883dbe5734..6a0e544afafbf 100644
--- a/pandas/_config/config.py
+++ b/pandas/_config/config.py
@@ -60,8 +60,10 @@
Any,
Callable,
Generic,
+ Literal,
NamedTuple,
cast,
+ overload,
)
import warnings
@@ -740,7 +742,23 @@ def _build_option_description(k: str) -> str:
return s
-def pp_options_list(keys: Iterable[str], width: int = 80, _print: bool = False):
+@overload
+def pp_options_list(
+ keys: Iterable[str], *, width: int = ..., _print: Literal[False] = ...
+) -> str:
+ ...
+
+
+@overload
+def pp_options_list(
+ keys: Iterable[str], *, width: int = ..., _print: Literal[True]
+) -> None:
+ ...
+
+
+def pp_options_list(
+ keys: Iterable[str], *, width: int = 80, _print: bool = False
+) -> str | None:
"""Builds a concise listing of available options, grouped by prefix"""
from itertools import groupby
from textwrap import wrap
@@ -770,8 +788,7 @@ def pp(name: str, ks: Iterable[str]) -> list[str]:
s = "\n".join(ls)
if _print:
print(s)
- else:
- return s
+ return s
#
diff --git a/pandas/_libs/arrays.pyi b/pandas/_libs/arrays.pyi
index 86f69c3cdfc75..dda23d9dec98b 100644
--- a/pandas/_libs/arrays.pyi
+++ b/pandas/_libs/arrays.pyi
@@ -14,9 +14,9 @@ class NDArrayBacked:
_ndarray: np.ndarray
def __init__(self, values: np.ndarray, dtype: DtypeObj) -> None: ...
@classmethod
- def _simple_new(cls, values: np.ndarray, dtype: DtypeObj): ...
- def _from_backing_data(self, values: np.ndarray): ...
- def __setstate__(self, state): ...
+ def _simple_new(cls, values: np.ndarray, dtype: DtypeObj) -> Self: ...
+ def _from_backing_data(self, values: np.ndarray) -> Self: ...
+ def __setstate__(self, state) -> None: ...
def __len__(self) -> int: ...
@property
def shape(self) -> Shape: ...
@@ -26,14 +26,14 @@ class NDArrayBacked:
def size(self) -> int: ...
@property
def nbytes(self) -> int: ...
- def copy(self, order=...): ...
- def delete(self, loc, axis=...): ...
- def swapaxes(self, axis1, axis2): ...
- def repeat(self, repeats: int | Sequence[int], axis: int | None = ...): ...
- def reshape(self, *args, **kwargs): ...
- def ravel(self, order=...): ...
+ def copy(self, order=...) -> Self: ...
+ def delete(self, loc, axis=...) -> Self: ...
+ def swapaxes(self, axis1, axis2) -> Self: ...
+ def repeat(self, repeats: int | Sequence[int], axis: int | None = ...) -> Self: ...
+ def reshape(self, *args, **kwargs) -> Self: ...
+ def ravel(self, order=...) -> Self: ...
@property
- def T(self): ...
+ def T(self) -> Self: ...
@classmethod
def _concat_same_type(
cls, to_concat: Sequence[Self], axis: AxisInt = ...
diff --git a/pandas/_libs/testing.pyi b/pandas/_libs/testing.pyi
index 01da496975f51..ab87e58eba9b9 100644
--- a/pandas/_libs/testing.pyi
+++ b/pandas/_libs/testing.pyi
@@ -1,4 +1,6 @@
-def assert_dict_equal(a, b, compare_keys: bool = ...): ...
+from typing import Mapping
+
+def assert_dict_equal(a: Mapping, b: Mapping, compare_keys: bool = ...) -> bool: ...
def assert_almost_equal(
a,
b,
@@ -9,4 +11,4 @@ def assert_almost_equal(
lobj=...,
robj=...,
index_values=...,
-): ...
+) -> bool: ...
diff --git a/pandas/_libs/tslibs/dtypes.pyi b/pandas/_libs/tslibs/dtypes.pyi
index 7fdeb88d498ac..dc2855cbecd69 100644
--- a/pandas/_libs/tslibs/dtypes.pyi
+++ b/pandas/_libs/tslibs/dtypes.pyi
@@ -1,5 +1,7 @@
from enum import Enum
+from pandas._typing import Self
+
OFFSET_TO_PERIOD_FREQSTR: dict[str, str]
def periods_per_day(reso: int = ...) -> int: ...
@@ -12,7 +14,7 @@ class PeriodDtypeBase:
_n: int
# actually __cinit__
- def __new__(cls, code: int, n: int): ...
+ def __new__(cls, code: int, n: int) -> Self: ...
@property
def _freq_group_code(self) -> int: ...
@property
diff --git a/pandas/_libs/tslibs/offsets.pyi b/pandas/_libs/tslibs/offsets.pyi
index 7eb8dc0813868..434f10a00745f 100644
--- a/pandas/_libs/tslibs/offsets.pyi
+++ b/pandas/_libs/tslibs/offsets.pyi
@@ -64,7 +64,7 @@ class BaseOffset:
@overload
def __rsub__(self, other: npt.NDArray[np.object_]) -> npt.NDArray[np.object_]: ...
@overload
- def __rsub__(self, other: BaseOffset): ...
+ def __rsub__(self, other: BaseOffset) -> Self: ...
@overload
def __rsub__(self, other: _DatetimeT) -> _DatetimeT: ...
@overload
@@ -72,7 +72,7 @@ class BaseOffset:
@overload
def __mul__(self, other: np.ndarray) -> np.ndarray: ...
@overload
- def __mul__(self, other: int): ...
+ def __mul__(self, other: int) -> Self: ...
@overload
def __rmul__(self, other: np.ndarray) -> np.ndarray: ...
@overload
@@ -100,7 +100,7 @@ def _get_offset(name: str) -> BaseOffset: ...
class SingleConstructorOffset(BaseOffset):
@classmethod
- def _from_name(cls, suffix: None = ...): ...
+ def _from_name(cls, suffix: None = ...) -> Self: ...
def __reduce__(self): ...
@overload
diff --git a/pandas/_testing/__init__.py b/pandas/_testing/__init__.py
index 3f5fd2e61b0cb..274de33738f5b 100644
--- a/pandas/_testing/__init__.py
+++ b/pandas/_testing/__init__.py
@@ -482,7 +482,7 @@ def iat(x):
_UNITS = ["s", "ms", "us", "ns"]
-def get_finest_unit(left: str, right: str):
+def get_finest_unit(left: str, right: str) -> str:
"""
Find the higher of two datetime64 units.
"""
diff --git a/pandas/core/arrays/_mixins.py b/pandas/core/arrays/_mixins.py
index 560845d375b56..adbb971f777b4 100644
--- a/pandas/core/arrays/_mixins.py
+++ b/pandas/core/arrays/_mixins.py
@@ -89,7 +89,9 @@ def method(self, *args, **kwargs):
return cast(F, method)
-class NDArrayBackedExtensionArray(NDArrayBacked, ExtensionArray):
+# error: Definition of "delete/ravel/T/repeat/copy" in base class "NDArrayBacked"
+# is incompatible with definition in base class "ExtensionArray"
+class NDArrayBackedExtensionArray(NDArrayBacked, ExtensionArray): # type: ignore[misc]
"""
ExtensionArray that is backed by a single NumPy ndarray.
"""
@@ -386,7 +388,7 @@ def fillna(
# ------------------------------------------------------------------------
# Reductions
- def _wrap_reduction_result(self, axis: AxisInt | None, result):
+ def _wrap_reduction_result(self, axis: AxisInt | None, result) -> Any:
if axis is None or self.ndim == 1:
return self._box_func(result)
return self._from_backing_data(result)
diff --git a/pandas/core/arrays/base.py b/pandas/core/arrays/base.py
index 58264f2aef6f3..b5946594fc7fc 100644
--- a/pandas/core/arrays/base.py
+++ b/pandas/core/arrays/base.py
@@ -1781,7 +1781,7 @@ def _formatter(self, boxed: bool = False) -> Callable[[Any], str | None]:
# Reshaping
# ------------------------------------------------------------------------
- def transpose(self, *axes: int) -> ExtensionArray:
+ def transpose(self, *axes: int) -> Self:
"""
Return a transposed view on this array.
@@ -1802,10 +1802,10 @@ def transpose(self, *axes: int) -> ExtensionArray:
return self[:]
@property
- def T(self) -> ExtensionArray:
+ def T(self) -> Self:
return self.transpose()
- def ravel(self, order: Literal["C", "F", "A", "K"] | None = "C") -> ExtensionArray:
+ def ravel(self, order: Literal["C", "F", "A", "K"] | None = "C") -> Self:
"""
Return a flattened view on this array.
diff --git a/pandas/core/arrays/categorical.py b/pandas/core/arrays/categorical.py
index 9d94cbbc5706e..09f506e117aaa 100644
--- a/pandas/core/arrays/categorical.py
+++ b/pandas/core/arrays/categorical.py
@@ -243,7 +243,9 @@ def contains(cat, key, container) -> bool:
return any(loc_ in container for loc_ in loc)
-class Categorical(NDArrayBackedExtensionArray, PandasObject, ObjectStringArrayMixin):
+# error: Definition of "delete/ravel/T/repeat/copy" in base class "NDArrayBacked"
+# is incompatible with definition in base class "ExtensionArray"
+class Categorical(NDArrayBackedExtensionArray, PandasObject, ObjectStringArrayMixin): # type: ignore[misc]
"""
Represent a categorical variable in classic R / S-plus fashion.
@@ -561,6 +563,7 @@ def astype(self, dtype: AstypeArg, copy: bool = True) -> ArrayLike:
object is returned.
"""
dtype = pandas_dtype(dtype)
+ result: Categorical | np.ndarray
if self.dtype is dtype:
result = self.copy() if copy else self
@@ -1818,10 +1821,14 @@ def value_counts(self, dropna: bool = True) -> Series:
ix = np.append(ix, -1)
ix = coerce_indexer_dtype(ix, self.dtype.categories)
- ix = self._from_backing_data(ix)
+ ix_categorical = self._from_backing_data(ix)
return Series(
- count, index=CategoricalIndex(ix), dtype="int64", name="count", copy=False
+ count,
+ index=CategoricalIndex(ix_categorical),
+ dtype="int64",
+ name="count",
+ copy=False,
)
# error: Argument 2 of "_empty" is incompatible with supertype
@@ -2526,7 +2533,9 @@ def _concat_same_type(cls, to_concat: Sequence[Self], axis: AxisInt = 0) -> Self
result = res_flat.reshape(len(first), -1, order="F")
return result
- result = union_categoricals(to_concat)
+ # error: Incompatible types in assignment (expression has type "Categorical",
+ # variable has type "Self")
+ result = union_categoricals(to_concat) # type: ignore[assignment]
return result
# ------------------------------------------------------------------
@@ -2666,11 +2675,11 @@ def _replace(self, *, to_replace, value, inplace: bool = False) -> Self | None:
new_categories = ser.take(locs)
new_categories = new_categories.drop_duplicates(keep="first")
- new_categories = Index(new_categories)
+ index_categories = Index(new_categories)
new_codes = recode_for_categories(
- cat._codes, all_values, new_categories, copy=False
+ cat._codes, all_values, index_categories, copy=False
)
- new_dtype = CategoricalDtype(new_categories, ordered=self.dtype.ordered)
+ new_dtype = CategoricalDtype(index_categories, ordered=self.dtype.ordered)
NDArrayBacked.__init__(cat, new_codes, new_dtype)
if new_dtype != orig_dtype:
diff --git a/pandas/core/arrays/interval.py b/pandas/core/arrays/interval.py
index 2dbc2a663c8a8..05e8b981f4e8a 100644
--- a/pandas/core/arrays/interval.py
+++ b/pandas/core/arrays/interval.py
@@ -1727,6 +1727,8 @@ def insert(self, loc: int, item: Interval) -> Self:
return self._shallow_copy(new_left, new_right)
def delete(self, loc) -> Self:
+ new_left: np.ndarray | DatetimeArray | TimedeltaArray
+ new_right: np.ndarray | DatetimeArray | TimedeltaArray
if isinstance(self._left, np.ndarray):
new_left = np.delete(self._left, loc)
assert isinstance(self._right, np.ndarray)
diff --git a/pandas/core/arrays/string_.py b/pandas/core/arrays/string_.py
index b73b49eca3e18..2272d7e38003d 100644
--- a/pandas/core/arrays/string_.py
+++ b/pandas/core/arrays/string_.py
@@ -499,10 +499,10 @@ def astype(self, dtype, copy: bool = True):
values = arr.astype(dtype.numpy_dtype)
return IntegerArray(values, mask, copy=False)
elif isinstance(dtype, FloatingDtype):
- arr = self.copy()
+ arr_ea = self.copy()
mask = self.isna()
- arr[mask] = "0"
- values = arr.astype(dtype.numpy_dtype)
+ arr_ea[mask] = "0"
+ values = arr_ea.astype(dtype.numpy_dtype)
return FloatingArray(values, mask, copy=False)
elif isinstance(dtype, ExtensionDtype):
# Skip the NumpyExtensionArray.astype method
diff --git a/pandas/plotting/_core.py b/pandas/plotting/_core.py
index 0f15b2edc0ca8..e8675a6d74746 100644
--- a/pandas/plotting/_core.py
+++ b/pandas/plotting/_core.py
@@ -98,7 +98,7 @@ def hist_series(
Returns
-------
- matplotlib.Axes
+ matplotlib.axes.Axes
A histogram plot.
See Also
diff --git a/pandas/plotting/_matplotlib/core.py b/pandas/plotting/_matplotlib/core.py
index 136056e3ff428..6fa75ba5fb12d 100644
--- a/pandas/plotting/_matplotlib/core.py
+++ b/pandas/plotting/_matplotlib/core.py
@@ -1021,7 +1021,7 @@ def _col_idx_to_axis_idx(self, col_idx: int) -> int:
return col_idx
@final
- def _get_ax(self, i: int):
+ def _get_ax(self, i: int) -> Axes:
# get the twinx ax if appropriate
if self.subplots:
i = self._col_idx_to_axis_idx(i)
@@ -1037,7 +1037,7 @@ def _get_ax(self, i: int):
return ax
@final
- def on_right(self, i: int):
+ def on_right(self, i: int) -> bool:
if isinstance(self.secondary_y, bool):
return self.secondary_y
@@ -1210,7 +1210,7 @@ def _get_errorbars(
return errors
@final
- def _get_subplots(self, fig: Figure):
+ def _get_subplots(self, fig: Figure) -> list[Axes]:
if Version(mpl.__version__) < Version("3.8"):
from matplotlib.axes import Subplot as Klass
else:
@@ -2100,9 +2100,11 @@ def blank_labeler(label, value):
results = ax.pie(y, labels=blabels, **kwds)
if kwds.get("autopct", None) is not None:
- patches, texts, autotexts = results
+ # error: Need more than 2 values to unpack (3 expected)
+ patches, texts, autotexts = results # type: ignore[misc]
else:
- patches, texts = results
+ # error: Too many values to unpack (2 expected, 3 provided)
+ patches, texts = results # type: ignore[misc]
autotexts = []
if self.fontsize is not None:
diff --git a/pandas/tseries/holiday.py b/pandas/tseries/holiday.py
index 646284c79a3ad..650e77b264d14 100644
--- a/pandas/tseries/holiday.py
+++ b/pandas/tseries/holiday.py
@@ -384,7 +384,7 @@ def register(cls) -> None:
holiday_calendars[name] = cls
-def get_calendar(name: str):
+def get_calendar(name: str) -> AbstractHolidayCalendar:
"""
Return an instance of a calendar based on its name.
@@ -433,7 +433,7 @@ def __init__(self, name: str = "", rules=None) -> None:
if rules is not None:
self.rules = rules
- def rule_from_name(self, name: str):
+ def rule_from_name(self, name: str) -> Holiday | None:
for rule in self.rules:
if rule.name == name:
return rule
diff --git a/pandas/util/_doctools.py b/pandas/util/_doctools.py
index 12619abf4baaf..828b7a4591bf2 100644
--- a/pandas/util/_doctools.py
+++ b/pandas/util/_doctools.py
@@ -9,6 +9,8 @@
if TYPE_CHECKING:
from collections.abc import Iterable
+ from matplotlib.figure import Figure
+
class TablePlotter:
"""
@@ -46,7 +48,9 @@ def _get_cells(self, left, right, vertical) -> tuple[int, int]:
hcells = sum([self._shape(df)[1] for df in left] + [self._shape(right)[1]])
return hcells, vcells
- def plot(self, left, right, labels: Iterable[str] = (), vertical: bool = True):
+ def plot(
+ self, left, right, labels: Iterable[str] = (), vertical: bool = True
+ ) -> Figure:
"""
Plot left / right DataFrames in specified layout.
| null | https://api.github.com/repos/pandas-dev/pandas/pulls/57108 | 2024-01-27T23:42:09Z | 2024-01-29T19:24:51Z | 2024-01-29T19:24:51Z | 2024-01-29T19:24:57Z |
DOC: Update meeseeksmachine reference | diff --git a/doc/source/development/maintaining.rst b/doc/source/development/maintaining.rst
index c7803d8401e4e..832e89c62e658 100644
--- a/doc/source/development/maintaining.rst
+++ b/doc/source/development/maintaining.rst
@@ -222,17 +222,17 @@ Since pandas minor releases are based on GitHub branches (e.g. point release of
"backporting" means merging a pull request fix to the ``main`` branch and correct minor branch associated with the next point release.
By default, if a pull request is assigned to the next point release milestone within the GitHub interface,
-the backporting process should happen automatically by the ``@meeseeksdev`` bot once the pull request is merged.
+the backporting process should happen automatically by the ``@meeseeksmachine`` bot once the pull request is merged.
A new pull request will be made backporting the pull request to the correct version branch.
Sometimes due to merge conflicts, a manual pull request will need to be made addressing the code conflict.
If the bot does not automatically start the backporting process, you can also write a GitHub comment in the merged pull request
to trigger the backport::
- @meeseeksdev backport version-branch
+ @meeseeksmachine backport version-branch
This will trigger a workflow which will backport a given change to a branch
-(e.g. @meeseeksdev backport 1.4.x)
+(e.g. @meeseeksmachine backport 2.2.x)
Cleaning up old issues
----------------------
| null | https://api.github.com/repos/pandas-dev/pandas/pulls/57107 | 2024-01-27T22:20:29Z | 2024-01-29T18:44:53Z | null | 2024-01-29T18:45:00Z |
Backport PR #57078 on branch 2.2.x (54628 fix find stack level memory leak) | diff --git a/pandas/_testing/_warnings.py b/pandas/_testing/_warnings.py
index f11dc11f6ac0d..c9a287942f2da 100644
--- a/pandas/_testing/_warnings.py
+++ b/pandas/_testing/_warnings.py
@@ -218,7 +218,12 @@ def _assert_raised_with_correct_stacklevel(
frame = inspect.currentframe()
for _ in range(4):
frame = frame.f_back # type: ignore[union-attr]
- caller_filename = inspect.getfile(frame) # type: ignore[arg-type]
+ try:
+ caller_filename = inspect.getfile(frame) # type: ignore[arg-type]
+ finally:
+ # See note in
+ # https://docs.python.org/3/library/inspect.html#inspect.Traceback
+ del frame
msg = (
"Warning not set with correct stacklevel. "
f"File where warning is raised: {actual_warning.filename} != "
diff --git a/pandas/util/_exceptions.py b/pandas/util/_exceptions.py
index 573f76a63459b..5f50838d37315 100644
--- a/pandas/util/_exceptions.py
+++ b/pandas/util/_exceptions.py
@@ -9,6 +9,7 @@
if TYPE_CHECKING:
from collections.abc import Generator
+ from types import FrameType
@contextlib.contextmanager
@@ -42,15 +43,20 @@ def find_stack_level() -> int:
test_dir = os.path.join(pkg_dir, "tests")
# https://stackoverflow.com/questions/17407119/python-inspect-stack-is-slow
- frame = inspect.currentframe()
- n = 0
- while frame:
- fname = inspect.getfile(frame)
- if fname.startswith(pkg_dir) and not fname.startswith(test_dir):
- frame = frame.f_back
- n += 1
- else:
- break
+ frame: FrameType | None = inspect.currentframe()
+ try:
+ n = 0
+ while frame:
+ filename = inspect.getfile(frame)
+ if filename.startswith(pkg_dir) and not filename.startswith(test_dir):
+ frame = frame.f_back
+ n += 1
+ else:
+ break
+ finally:
+ # See note in
+ # https://docs.python.org/3/library/inspect.html#inspect.Traceback
+ del frame
return n
| Backport PR #57078: 54628 fix find stack level memory leak | https://api.github.com/repos/pandas-dev/pandas/pulls/57105 | 2024-01-27T18:03:22Z | 2024-01-27T19:38:02Z | 2024-01-27T19:38:02Z | 2024-01-27T19:38:02Z |
ENH: Add check for character limmit | diff --git a/doc/source/whatsnew/v3.0.0.rst b/doc/source/whatsnew/v3.0.0.rst
index 6d41c29a6c614..ec6172b4a7ba6 100644
--- a/doc/source/whatsnew/v3.0.0.rst
+++ b/doc/source/whatsnew/v3.0.0.rst
@@ -28,7 +28,7 @@ enhancement2
Other enhancements
^^^^^^^^^^^^^^^^^^
--
+- :func:`DataFrame.to_excel` now raises an ``UserWarning`` when the character count in a cell exceeds Excel's limitation of 32767 characters (:issue:`56954`)
-
.. ---------------------------------------------------------------------------
diff --git a/pandas/core/generic.py b/pandas/core/generic.py
index 87a394761ee7c..9a65e53722d57 100644
--- a/pandas/core/generic.py
+++ b/pandas/core/generic.py
@@ -2363,6 +2363,10 @@ def to_excel(
Once a workbook has been saved it is not possible to write further
data without rewriting the whole workbook.
+ pandas will check the number of rows, columns,
+ and cell character count does not exceed Excel's limitations.
+ All other limitations must be checked by the user.
+
Examples
--------
diff --git a/pandas/io/excel/_base.py b/pandas/io/excel/_base.py
index c7642236d4b2a..4109b6d0965bb 100644
--- a/pandas/io/excel/_base.py
+++ b/pandas/io/excel/_base.py
@@ -1326,7 +1326,15 @@ def _value_with_fmt(
fmt = "0"
else:
val = str(val)
-
+ # GH#56954
+ # Excel's limitation on cell contents is 32767 characters
+ # xref https://support.microsoft.com/en-au/office/excel-specifications-and-limits-1672b34d-7043-467e-8e27-269d656771c3
+ if len(val) > 32767:
+ warnings.warn(
+ "Cell contents too long, truncated to 32767 characters",
+ UserWarning,
+ stacklevel=find_stack_level(),
+ )
return val, fmt
@classmethod
diff --git a/pandas/tests/io/excel/test_writers.py b/pandas/tests/io/excel/test_writers.py
index 6aea7464ee8dc..6ea48cd759fbc 100644
--- a/pandas/tests/io/excel/test_writers.py
+++ b/pandas/tests/io/excel/test_writers.py
@@ -1385,6 +1385,18 @@ def test_to_excel_empty_frame(self, engine, ext):
expected = DataFrame()
tm.assert_frame_equal(result, expected)
+ def test_to_excel_raising_warning_when_cell_character_exceed_limit(
+ self, path, engine
+ ):
+ # GH#56954
+ df = DataFrame({"A": ["a" * 32768]})
+ msg = "Cell contents too long, truncated to 32767 characters"
+ with tm.assert_produces_warning(
+ UserWarning, match=msg, raise_on_extra_warnings=False
+ ):
+ buf = BytesIO()
+ df.to_excel(buf)
+
class TestExcelWriterEngineTests:
@pytest.mark.parametrize(
| - [x] closes #56954 (Replace xxxx with the GitHub issue number)
- [x] [Tests added and passed](https://pandas.pydata.org/pandas-docs/dev/development/contributing_codebase.html#writing-tests) if fixing a bug or adding a new feature
- [ ] All [code checks passed](https://pandas.pydata.org/pandas-docs/dev/development/contributing_codebase.html#pre-commit).
- [ ] Added [type annotations](https://pandas.pydata.org/pandas-docs/dev/development/contributing_codebase.html#type-hints) to new arguments/methods/functions.
- [ ] Added an entry in the latest `doc/source/whatsnew/vX.X.X.rst` file if fixing a bug or adding a new feature.
| https://api.github.com/repos/pandas-dev/pandas/pulls/57103 | 2024-01-27T08:07:57Z | 2024-02-01T21:37:06Z | 2024-02-01T21:37:06Z | 2024-02-02T02:27:57Z |
ENH: Add skipna to groupby.first and groupby.last | diff --git a/doc/source/whatsnew/v2.2.1.rst b/doc/source/whatsnew/v2.2.1.rst
index ff5fadec735e6..589903ddcca71 100644
--- a/doc/source/whatsnew/v2.2.1.rst
+++ b/doc/source/whatsnew/v2.2.1.rst
@@ -34,7 +34,8 @@ Bug fixes
Other
~~~~~
--
+- Added the argument ``skipna`` to :meth:`DataFrameGroupBy.first`, :meth:`DataFrameGroupBy.last`, :meth:`SeriesGroupBy.first`, and :meth:`SeriesGroupBy.last`; achieving ``skipna=False`` used to be available via :meth:`DataFrameGroupBy.nth`, but the behavior was changed in pandas 2.0.0 (:issue:`57019`)
+- Added the argument ``skipna`` to :meth:`Resampler.first`, :meth:`Resampler.last` (:issue:`57019`)
.. ---------------------------------------------------------------------------
.. _whatsnew_221.contributors:
diff --git a/pandas/_libs/groupby.pyi b/pandas/_libs/groupby.pyi
index b7130ee35dc57..95ac555303221 100644
--- a/pandas/_libs/groupby.pyi
+++ b/pandas/_libs/groupby.pyi
@@ -136,6 +136,7 @@ def group_last(
result_mask: npt.NDArray[np.bool_] | None = ...,
min_count: int = ..., # Py_ssize_t
is_datetimelike: bool = ...,
+ skipna: bool = ...,
) -> None: ...
def group_nth(
out: np.ndarray, # rank_t[:, ::1]
@@ -147,6 +148,7 @@ def group_nth(
min_count: int = ..., # int64_t
rank: int = ..., # int64_t
is_datetimelike: bool = ...,
+ skipna: bool = ...,
) -> None: ...
def group_rank(
out: np.ndarray, # float64_t[:, ::1]
diff --git a/pandas/_libs/groupby.pyx b/pandas/_libs/groupby.pyx
index 45e02c3dd420f..391bb4a3a3fd3 100644
--- a/pandas/_libs/groupby.pyx
+++ b/pandas/_libs/groupby.pyx
@@ -1428,6 +1428,7 @@ def group_last(
uint8_t[:, ::1] result_mask=None,
Py_ssize_t min_count=-1,
bint is_datetimelike=False,
+ bint skipna=True,
) -> None:
"""
Only aggregates on axis=0
@@ -1462,14 +1463,19 @@ def group_last(
for j in range(K):
val = values[i, j]
- if uses_mask:
- isna_entry = mask[i, j]
- else:
- isna_entry = _treat_as_na(val, is_datetimelike)
+ if skipna:
+ if uses_mask:
+ isna_entry = mask[i, j]
+ else:
+ isna_entry = _treat_as_na(val, is_datetimelike)
+ if isna_entry:
+ continue
- if not isna_entry:
- nobs[lab, j] += 1
- resx[lab, j] = val
+ nobs[lab, j] += 1
+ resx[lab, j] = val
+
+ if uses_mask and not skipna:
+ result_mask[lab, j] = mask[i, j]
_check_below_mincount(
out, uses_mask, result_mask, ncounts, K, nobs, min_count, resx
@@ -1490,6 +1496,7 @@ def group_nth(
int64_t min_count=-1,
int64_t rank=1,
bint is_datetimelike=False,
+ bint skipna=True,
) -> None:
"""
Only aggregates on axis=0
@@ -1524,15 +1531,19 @@ def group_nth(
for j in range(K):
val = values[i, j]
- if uses_mask:
- isna_entry = mask[i, j]
- else:
- isna_entry = _treat_as_na(val, is_datetimelike)
+ if skipna:
+ if uses_mask:
+ isna_entry = mask[i, j]
+ else:
+ isna_entry = _treat_as_na(val, is_datetimelike)
+ if isna_entry:
+ continue
- if not isna_entry:
- nobs[lab, j] += 1
- if nobs[lab, j] == rank:
- resx[lab, j] = val
+ nobs[lab, j] += 1
+ if nobs[lab, j] == rank:
+ resx[lab, j] = val
+ if uses_mask and not skipna:
+ result_mask[lab, j] = mask[i, j]
_check_below_mincount(
out, uses_mask, result_mask, ncounts, K, nobs, min_count, resx
diff --git a/pandas/_testing/__init__.py b/pandas/_testing/__init__.py
index 274de33738f5b..b4cd6d82bb57f 100644
--- a/pandas/_testing/__init__.py
+++ b/pandas/_testing/__init__.py
@@ -235,11 +235,18 @@
+ TIMEDELTA_PYARROW_DTYPES
+ BOOL_PYARROW_DTYPES
)
+ ALL_REAL_PYARROW_DTYPES_STR_REPR = (
+ ALL_INT_PYARROW_DTYPES_STR_REPR + FLOAT_PYARROW_DTYPES_STR_REPR
+ )
else:
FLOAT_PYARROW_DTYPES_STR_REPR = []
ALL_INT_PYARROW_DTYPES_STR_REPR = []
ALL_PYARROW_DTYPES = []
+ ALL_REAL_PYARROW_DTYPES_STR_REPR = []
+ALL_REAL_NULLABLE_DTYPES = (
+ FLOAT_NUMPY_DTYPES + ALL_REAL_EXTENSION_DTYPES + ALL_REAL_PYARROW_DTYPES_STR_REPR
+)
arithmetic_dunder_methods = [
"__add__",
diff --git a/pandas/conftest.py b/pandas/conftest.py
index 94805313ccfc1..c5dc48b9ed096 100644
--- a/pandas/conftest.py
+++ b/pandas/conftest.py
@@ -1703,6 +1703,38 @@ def any_numpy_dtype(request):
return request.param
+@pytest.fixture(params=tm.ALL_REAL_NULLABLE_DTYPES)
+def any_real_nullable_dtype(request):
+ """
+ Parameterized fixture for all real dtypes that can hold NA.
+
+ * float
+ * 'float32'
+ * 'float64'
+ * 'Float32'
+ * 'Float64'
+ * 'UInt8'
+ * 'UInt16'
+ * 'UInt32'
+ * 'UInt64'
+ * 'Int8'
+ * 'Int16'
+ * 'Int32'
+ * 'Int64'
+ * 'uint8[pyarrow]'
+ * 'uint16[pyarrow]'
+ * 'uint32[pyarrow]'
+ * 'uint64[pyarrow]'
+ * 'int8[pyarrow]'
+ * 'int16[pyarrow]'
+ * 'int32[pyarrow]'
+ * 'int64[pyarrow]'
+ * 'float[pyarrow]'
+ * 'double[pyarrow]'
+ """
+ return request.param
+
+
@pytest.fixture(params=tm.ALL_NUMERIC_DTYPES)
def any_numeric_dtype(request):
"""
diff --git a/pandas/core/groupby/groupby.py b/pandas/core/groupby/groupby.py
index b2afaffc267fa..7227d5c727994 100644
--- a/pandas/core/groupby/groupby.py
+++ b/pandas/core/groupby/groupby.py
@@ -3364,9 +3364,13 @@ def max(
)
@final
- def first(self, numeric_only: bool = False, min_count: int = -1) -> NDFrameT:
+ def first(
+ self, numeric_only: bool = False, min_count: int = -1, skipna: bool = True
+ ) -> NDFrameT:
"""
- Compute the first non-null entry of each column.
+ Compute the first entry of each column within each group.
+
+ Defaults to skipping NA elements.
Parameters
----------
@@ -3374,12 +3378,17 @@ def first(self, numeric_only: bool = False, min_count: int = -1) -> NDFrameT:
Include only float, int, boolean columns.
min_count : int, default -1
The required number of valid values to perform the operation. If fewer
- than ``min_count`` non-NA values are present the result will be NA.
+ than ``min_count`` valid values are present the result will be NA.
+ skipna : bool, default True
+ Exclude NA/null values. If an entire row/column is NA, the result
+ will be NA.
+
+ .. versionadded:: 2.2.1
Returns
-------
Series or DataFrame
- First non-null of values within each group.
+ First values within each group.
See Also
--------
@@ -3431,12 +3440,17 @@ def first(x: Series):
min_count=min_count,
alias="first",
npfunc=first_compat,
+ skipna=skipna,
)
@final
- def last(self, numeric_only: bool = False, min_count: int = -1) -> NDFrameT:
+ def last(
+ self, numeric_only: bool = False, min_count: int = -1, skipna: bool = True
+ ) -> NDFrameT:
"""
- Compute the last non-null entry of each column.
+ Compute the last entry of each column within each group.
+
+ Defaults to skipping NA elements.
Parameters
----------
@@ -3445,12 +3459,17 @@ def last(self, numeric_only: bool = False, min_count: int = -1) -> NDFrameT:
everything, then use only numeric data.
min_count : int, default -1
The required number of valid values to perform the operation. If fewer
- than ``min_count`` non-NA values are present the result will be NA.
+ than ``min_count`` valid values are present the result will be NA.
+ skipna : bool, default True
+ Exclude NA/null values. If an entire row/column is NA, the result
+ will be NA.
+
+ .. versionadded:: 2.2.1
Returns
-------
Series or DataFrame
- Last non-null of values within each group.
+ Last of values within each group.
See Also
--------
@@ -3490,6 +3509,7 @@ def last(x: Series):
min_count=min_count,
alias="last",
npfunc=last_compat,
+ skipna=skipna,
)
@final
diff --git a/pandas/core/resample.py b/pandas/core/resample.py
index 082196abc17c2..4d6507d89ec90 100644
--- a/pandas/core/resample.py
+++ b/pandas/core/resample.py
@@ -1329,12 +1329,15 @@ def first(
self,
numeric_only: bool = False,
min_count: int = 0,
+ skipna: bool = True,
*args,
**kwargs,
):
maybe_warn_args_and_kwargs(type(self), "first", args, kwargs)
nv.validate_resampler_func("first", args, kwargs)
- return self._downsample("first", numeric_only=numeric_only, min_count=min_count)
+ return self._downsample(
+ "first", numeric_only=numeric_only, min_count=min_count, skipna=skipna
+ )
@final
@doc(GroupBy.last)
@@ -1342,12 +1345,15 @@ def last(
self,
numeric_only: bool = False,
min_count: int = 0,
+ skipna: bool = True,
*args,
**kwargs,
):
maybe_warn_args_and_kwargs(type(self), "last", args, kwargs)
nv.validate_resampler_func("last", args, kwargs)
- return self._downsample("last", numeric_only=numeric_only, min_count=min_count)
+ return self._downsample(
+ "last", numeric_only=numeric_only, min_count=min_count, skipna=skipna
+ )
@final
@doc(GroupBy.median)
diff --git a/pandas/tests/groupby/test_reductions.py b/pandas/tests/groupby/test_reductions.py
index d24a2a26bba81..bd188c729846c 100644
--- a/pandas/tests/groupby/test_reductions.py
+++ b/pandas/tests/groupby/test_reductions.py
@@ -7,6 +7,9 @@
from pandas._libs.tslibs import iNaT
+from pandas.core.dtypes.common import pandas_dtype
+from pandas.core.dtypes.missing import na_value_for_dtype
+
import pandas as pd
from pandas import (
DataFrame,
@@ -389,6 +392,34 @@ def test_groupby_non_arithmetic_agg_int_like_precision(method, data):
tm.assert_frame_equal(result, expected)
+@pytest.mark.parametrize("how", ["first", "last"])
+def test_first_last_skipna(any_real_nullable_dtype, sort, skipna, how):
+ # GH#57019
+ na_value = na_value_for_dtype(pandas_dtype(any_real_nullable_dtype))
+ df = DataFrame(
+ {
+ "a": [2, 1, 1, 2, 3, 3],
+ "b": [na_value, 3.0, na_value, 4.0, np.nan, np.nan],
+ "c": [na_value, 3.0, na_value, 4.0, np.nan, np.nan],
+ },
+ dtype=any_real_nullable_dtype,
+ )
+ gb = df.groupby("a", sort=sort)
+ method = getattr(gb, how)
+ result = method(skipna=skipna)
+
+ ilocs = {
+ ("first", True): [3, 1, 4],
+ ("first", False): [0, 1, 4],
+ ("last", True): [3, 1, 5],
+ ("last", False): [3, 2, 5],
+ }[how, skipna]
+ expected = df.iloc[ilocs].set_index("a")
+ if sort:
+ expected = expected.sort_index()
+ tm.assert_frame_equal(result, expected)
+
+
def test_idxmin_idxmax_axis1():
df = DataFrame(
np.random.default_rng(2).standard_normal((10, 4)), columns=["A", "B", "C", "D"]
diff --git a/pandas/tests/resample/test_base.py b/pandas/tests/resample/test_base.py
index ab75dd7469b73..9cd51b95d6efd 100644
--- a/pandas/tests/resample/test_base.py
+++ b/pandas/tests/resample/test_base.py
@@ -3,6 +3,9 @@
import numpy as np
import pytest
+from pandas.core.dtypes.common import is_extension_array_dtype
+
+import pandas as pd
from pandas import (
DataFrame,
DatetimeIndex,
@@ -459,3 +462,29 @@ def test_resample_quantile(index):
result = ser.resample(freq).quantile(q)
expected = ser.resample(freq).agg(lambda x: x.quantile(q)).rename(ser.name)
tm.assert_series_equal(result, expected)
+
+
+@pytest.mark.parametrize("how", ["first", "last"])
+def test_first_last_skipna(any_real_nullable_dtype, skipna, how):
+ # GH#57019
+ if is_extension_array_dtype(any_real_nullable_dtype):
+ na_value = Series(dtype=any_real_nullable_dtype).dtype.na_value
+ else:
+ na_value = np.nan
+ df = DataFrame(
+ {
+ "a": [2, 1, 1, 2],
+ "b": [na_value, 3.0, na_value, 4.0],
+ "c": [na_value, 3.0, na_value, 4.0],
+ },
+ index=date_range("2020-01-01", periods=4, freq="D"),
+ dtype=any_real_nullable_dtype,
+ )
+ rs = df.resample("ME")
+ method = getattr(rs, how)
+ result = method(skipna=skipna)
+
+ gb = df.groupby(df.shape[0] * [pd.to_datetime("2020-01-31")])
+ expected = getattr(gb, how)(skipna=skipna)
+ expected.index.freq = "ME"
+ tm.assert_frame_equal(result, expected)
diff --git a/pandas/tests/resample/test_resample_api.py b/pandas/tests/resample/test_resample_api.py
index d3e906827b754..12abd1c98784b 100644
--- a/pandas/tests/resample/test_resample_api.py
+++ b/pandas/tests/resample/test_resample_api.py
@@ -1040,11 +1040,11 @@ def test_args_kwargs_depr(method, raises):
if raises:
with tm.assert_produces_warning(FutureWarning, match=warn_msg):
with pytest.raises(UnsupportedFunctionCall, match=error_msg):
- func(*args, 1, 2, 3)
+ func(*args, 1, 2, 3, 4)
else:
with tm.assert_produces_warning(FutureWarning, match=warn_msg):
with pytest.raises(TypeError, match=error_msg_type):
- func(*args, 1, 2, 3)
+ func(*args, 1, 2, 3, 4)
def test_df_axis_param_depr():
| - [x] closes #57019 (Replace xxxx with the GitHub issue number)
- [ ] [Tests added and passed](https://pandas.pydata.org/pandas-docs/dev/development/contributing_codebase.html#writing-tests) if fixing a bug or adding a new feature
- [ ] All [code checks passed](https://pandas.pydata.org/pandas-docs/dev/development/contributing_codebase.html#pre-commit).
- [ ] Added [type annotations](https://pandas.pydata.org/pandas-docs/dev/development/contributing_codebase.html#type-hints) to new arguments/methods/functions.
- [ ] Added an entry in the latest `doc/source/whatsnew/vX.X.X.rst` file if fixing a bug or adding a new feature.
There is a case to put this in 2.2.1 as the ability to agg the first element from every group including NA values was removed by changing the behavior of nth in pandas 2.0.0. But also okay to put this in 3.0. | https://api.github.com/repos/pandas-dev/pandas/pulls/57102 | 2024-01-27T04:49:08Z | 2024-01-30T02:14:25Z | 2024-01-30T02:14:25Z | 2024-01-30T03:14:53Z |
REGR: Index.join raising TypeError when joining an empty index to a mixed type index | diff --git a/doc/source/whatsnew/v2.2.1.rst b/doc/source/whatsnew/v2.2.1.rst
index b9b2821ebc468..441d98e6ea106 100644
--- a/doc/source/whatsnew/v2.2.1.rst
+++ b/doc/source/whatsnew/v2.2.1.rst
@@ -18,6 +18,7 @@ Fixed regressions
- Fixed regression in :func:`merge_ordered` raising ``TypeError`` for ``fill_method="ffill"`` and ``how="left"`` (:issue:`57010`)
- Fixed regression in :meth:`DataFrameGroupBy.idxmin`, :meth:`DataFrameGroupBy.idxmax`, :meth:`SeriesGroupBy.idxmin`, :meth:`SeriesGroupBy.idxmax` ignoring the ``skipna`` argument (:issue:`57040`)
- Fixed regression in :meth:`DataFrameGroupBy.idxmin`, :meth:`DataFrameGroupBy.idxmax`, :meth:`SeriesGroupBy.idxmin`, :meth:`SeriesGroupBy.idxmax` where values containing the minimum or maximum value for the dtype could produce incorrect results (:issue:`57040`)
+- Fixed regression in :meth:`Index.join` raising ``TypeError`` when joining an empty index to a non-empty index containing mixed dtype values (:issue:`57048`)
- Fixed regression in :meth:`Series.pct_change` raising a ``ValueError`` for an empty :class:`Series` (:issue:`57056`)
.. ---------------------------------------------------------------------------
diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py
index bf20525e2c7fd..b2c3227f4c379 100644
--- a/pandas/core/indexes/base.py
+++ b/pandas/core/indexes/base.py
@@ -4610,38 +4610,12 @@ def join(
if level is not None and (self._is_multi or other._is_multi):
return self._join_level(other, level, how=how)
- lidx: np.ndarray | None
- ridx: np.ndarray | None
-
- if len(other) == 0:
- if how in ("left", "outer"):
- if sort and not self.is_monotonic_increasing:
- lidx = self.argsort()
- join_index = self.take(lidx)
- else:
- lidx = None
- join_index = self._view()
- ridx = np.broadcast_to(np.intp(-1), len(join_index))
- return join_index, lidx, ridx
- elif how in ("right", "inner", "cross"):
- join_index = other._view()
- lidx = np.array([], dtype=np.intp)
- return join_index, lidx, None
-
- if len(self) == 0:
- if how in ("right", "outer"):
- if sort and not other.is_monotonic_increasing:
- ridx = other.argsort()
- join_index = other.take(ridx)
- else:
- ridx = None
- join_index = other._view()
- lidx = np.broadcast_to(np.intp(-1), len(join_index))
- return join_index, lidx, ridx
- elif how in ("left", "inner", "cross"):
- join_index = self._view()
- ridx = np.array([], dtype=np.intp)
- return join_index, None, ridx
+ if len(self) == 0 or len(other) == 0:
+ try:
+ return self._join_empty(other, how, sort)
+ except TypeError:
+ # object dtype; non-comparable objects
+ pass
if self.dtype != other.dtype:
dtype = self._find_common_type_compat(other)
@@ -4676,6 +4650,33 @@ def join(
return self._join_via_get_indexer(other, how, sort)
+ @final
+ def _join_empty(
+ self, other: Index, how: JoinHow, sort: bool
+ ) -> tuple[Index, npt.NDArray[np.intp] | None, npt.NDArray[np.intp] | None]:
+ assert len(self) == 0 or len(other) == 0
+ _validate_join_method(how)
+
+ lidx: np.ndarray | None
+ ridx: np.ndarray | None
+
+ if len(other):
+ how = cast(JoinHow, {"left": "right", "right": "left"}.get(how, how))
+ join_index, ridx, lidx = other._join_empty(self, how, sort)
+ elif how in ["left", "outer"]:
+ if sort and not self.is_monotonic_increasing:
+ lidx = self.argsort()
+ join_index = self.take(lidx)
+ else:
+ lidx = None
+ join_index = self._view()
+ ridx = np.broadcast_to(np.intp(-1), len(join_index))
+ else:
+ join_index = other._view()
+ lidx = np.array([], dtype=np.intp)
+ ridx = None
+ return join_index, lidx, ridx
+
@final
def _join_via_get_indexer(
self, other: Index, how: JoinHow, sort: bool
diff --git a/pandas/tests/reshape/merge/test_join.py b/pandas/tests/reshape/merge/test_join.py
index 24e44c8f9e53e..883a50a0c1399 100644
--- a/pandas/tests/reshape/merge/test_join.py
+++ b/pandas/tests/reshape/merge/test_join.py
@@ -1041,6 +1041,25 @@ def test_join_empty(left_empty, how, exp):
tm.assert_frame_equal(result, expected)
+def test_join_empty_uncomparable_columns():
+ # GH 57048
+ df1 = DataFrame()
+ df2 = DataFrame(columns=["test"])
+ df3 = DataFrame(columns=["foo", ("bar", "baz")])
+
+ result = df1 + df2
+ expected = DataFrame(columns=["test"])
+ tm.assert_frame_equal(result, expected)
+
+ result = df2 + df3
+ expected = DataFrame(columns=[("bar", "baz"), "foo", "test"])
+ tm.assert_frame_equal(result, expected)
+
+ result = df1 + df3
+ expected = DataFrame(columns=[("bar", "baz"), "foo"])
+ tm.assert_frame_equal(result, expected)
+
+
@pytest.mark.parametrize(
"how, values",
[
| - [x] closes #57048
- [x] [Tests added and passed](https://pandas.pydata.org/pandas-docs/dev/development/contributing_codebase.html#writing-tests) if fixing a bug or adding a new feature
- [x] All [code checks passed](https://pandas.pydata.org/pandas-docs/dev/development/contributing_codebase.html#pre-commit).
- [x] Added [type annotations](https://pandas.pydata.org/pandas-docs/dev/development/contributing_codebase.html#type-hints) to new arguments/methods/functions.
- [x] Added an entry in the latest `doc/source/whatsnew/v2.2.1.rst` file if fixing a bug or adding a new feature.
| https://api.github.com/repos/pandas-dev/pandas/pulls/57101 | 2024-01-27T03:30:35Z | 2024-01-29T19:53:42Z | 2024-01-29T19:53:41Z | 2024-01-29T19:53:48Z |
DOCS: DataFrame.any() and DataFrame.all() documentation specifies the… | diff --git a/pandas/core/generic.py b/pandas/core/generic.py
index 91a4271509421..d64404c0f3924 100644
--- a/pandas/core/generic.py
+++ b/pandas/core/generic.py
@@ -13081,8 +13081,8 @@ def last_valid_index(self) -> Hashable | None:
Returns
-------
{name1} or {name2}
- If level is specified, then, {name2} is returned; otherwise, {name1}
- is returned.
+ If axis=None, then a {name2} boolean is returned. Otherwise a Series is
+ returned with index matching the index argument.
{see_also}
{examples}"""
@@ -13643,12 +13643,16 @@ def make_doc(name: str, ndim: int) -> str:
axis_descr = "{index (0), columns (1)}"
if name == "any":
+ name1 = "DataFrame"
+ name2 = "scalar"
base_doc = _bool_doc
desc = _any_desc
see_also = _any_see_also
examples = _any_examples
kwargs = {"empty_value": "False"}
elif name == "all":
+ name1 = "DataFrame"
+ name2 = "scalar"
base_doc = _bool_doc
desc = _all_desc
see_also = _all_see_also
| … wrong return types
- [x] closes #57088
- [x] [Tests added and passed](https://pandas.pydata.org/pandas-docs/dev/development/contributing_codebase.html#writing-tests) if fixing a bug or adding a new feature
- [x] All [code checks passed](https://pandas.pydata.org/pandas-docs/dev/development/contributing_codebase.html#pre-commit).
- [x] Added [type annotations](https://pandas.pydata.org/pandas-docs/dev/development/contributing_codebase.html#type-hints) to new arguments/methods/functions.
- [x] Added an entry in the latest `doc/source/whatsnew/vX.X.X.rst` file if fixing a bug or adding a new feature.
| https://api.github.com/repos/pandas-dev/pandas/pulls/57098 | 2024-01-27T01:49:13Z | 2024-02-29T00:56:43Z | null | 2024-02-29T00:56:43Z |
Remove parts that reference bfill,ffill,pad,backfill in limit_direction | diff --git a/pandas/core/resample.py b/pandas/core/resample.py
index b3e86b4360d74..5149d2a2eba30 100644
--- a/pandas/core/resample.py
+++ b/pandas/core/resample.py
@@ -977,20 +977,6 @@ def interpolate(
limit_direction : {{'forward', 'backward', 'both'}}, Optional
Consecutive NaNs will be filled in this direction.
- If limit is specified:
- * If 'method' is 'pad' or 'ffill', 'limit_direction' must be 'forward'.
- * If 'method' is 'backfill' or 'bfill', 'limit_direction' must be
- 'backwards'.
-
- If 'limit' is not specified:
- * If 'method' is 'backfill' or 'bfill', the default is 'backward'
- * else the default is 'forward'
-
- raises ValueError if `limit_direction` is 'forward' or 'both' and
- method is 'backfill' or 'bfill'.
- raises ValueError if `limit_direction` is 'backward' or 'both' and
- method is 'pad' or 'ffill'.
-
limit_area : {{`None`, 'inside', 'outside'}}, default None
If limit is specified, consecutive NaNs will be filled with this
restriction.
| This PR is related to the issue #57092. It removes the parts that reference `bfill`, `ffill`, `pad`, and `backfill` as allowable methods under limit_direction as they are not supported.
- [x] closes #57092
- [ ] [Tests added and passed](https://pandas.pydata.org/pandas-docs/dev/development/contributing_codebase.html#writing-tests) if fixing a bug or adding a new feature
- [ ] All [code checks passed](https://pandas.pydata.org/pandas-docs/dev/development/contributing_codebase.html#pre-commit).
- [ ] Added [type annotations](https://pandas.pydata.org/pandas-docs/dev/development/contributing_codebase.html#type-hints) to new arguments/methods/functions.
- [ ] Added an entry in the latest `doc/source/whatsnew/vX.X.X.rst` file if fixing a bug or adding a new feature.
| https://api.github.com/repos/pandas-dev/pandas/pulls/57097 | 2024-01-27T01:27:51Z | 2024-02-28T17:45:18Z | null | 2024-02-28T17:45:18Z |
CLN: Move capitalize_first_letter to where it's used | diff --git a/pandas/core/dtypes/dtypes.py b/pandas/core/dtypes/dtypes.py
index 5b51bc9debb33..a6a5f142faf1c 100644
--- a/pandas/core/dtypes/dtypes.py
+++ b/pandas/core/dtypes/dtypes.py
@@ -62,8 +62,6 @@
is_list_like,
)
-from pandas.util import capitalize_first_letter
-
if not pa_version_under10p1:
import pyarrow as pa
@@ -1087,7 +1085,7 @@ def na_value(self) -> NaTType:
def __eq__(self, other: object) -> bool:
if isinstance(other, str):
- return other in [self.name, capitalize_first_letter(self.name)]
+ return other[:1].lower() + other[1:] == self.name
return super().__eq__(other)
diff --git a/pandas/util/__init__.py b/pandas/util/__init__.py
index 59ab324ba38ca..da109a514433f 100644
--- a/pandas/util/__init__.py
+++ b/pandas/util/__init__.py
@@ -27,7 +27,3 @@ def __getattr__(key: str):
def __dir__():
return list(globals().keys()) + ["hash_array", "hash_pandas_object"]
-
-
-def capitalize_first_letter(s: str) -> str:
- return s[:1].upper() + s[1:]
| null | https://api.github.com/repos/pandas-dev/pandas/pulls/57096 | 2024-01-26T23:08:47Z | 2024-02-02T13:43:57Z | 2024-02-02T13:43:57Z | 2024-02-02T17:42:25Z |
CVE 2020 13091 | diff --git a/INFO b/INFO
new file mode 100644
index 0000000000000..6e7aee30b3484
--- /dev/null
+++ b/INFO
@@ -0,0 +1,30 @@
+### tests
+
+updated /tests/io/test_pickle.py
+did not write the compatibility tests for older pandas versions yet
+
+###
+
+# call this file pickle_config.yml
+# this is meant to configure pickle deserialization, which can be a security risk
+
+# mode can be permit, deny, off
+
+# permit means pickle is only allowed to load permitted libraries (safer option)
+# deny means pickle is not allowed to load denied libraries (less safe option)
+# off means no security and should only be used on safe pickles (least safe option)
+mode: permit
+
+# here you would list packages and classes that can be used
+# this is how Python recommend you restrict pickling globals
+# see https://docs.python.org/3/library/pickle.html#restricting-globals
+permit:
+ builtins: ['range', 'complex', 'set', 'frozenset', 'slice']
+
+# here you would list packages and classes than cannot be used
+# all others will be allowed if running on deny mode
+# this is not as safe as the permit method
+# see https://pythonmana.com/2022/143/202205231222219535.html
+deny:
+ builtins: ['eval', 'exec', 'execfile', 'compile', 'open', 'input', '__import__', 'exit']
+ os: ['system']
diff --git a/pandas/compat/pickle_compat.py b/pandas/compat/pickle_compat.py
index cd98087c06c18..185ebb492575c 100644
--- a/pandas/compat/pickle_compat.py
+++ b/pandas/compat/pickle_compat.py
@@ -11,6 +11,8 @@
import numpy as np
+from pandas._config import get_option
+
from pandas._libs.arrays import NDArrayBacked
from pandas._libs.tslibs import BaseOffset
@@ -152,6 +154,7 @@ def load_reduce(self) -> None:
# our Unpickler sub-class to override methods and some dispatcher
# functions for compat and uses a non-public class of the pickle module.
+# checks modules against permit/deny list and raises error if module is not forbidden.
class Unpickler(pkl._Unpickler):
@@ -159,7 +162,20 @@ def find_class(self, module, name):
# override superclass
key = (module, name)
module, name = _class_locations_map.get(key, key)
- return super().find_class(module, name)
+ opt = get_option("pickler.unpickle.mode")
+ # Only allow safe modules and classes. Tuples defined in config
+ # Do not allow unsafe modules and classes.
+ if (
+ (opt == "off")
+ or (opt == "permit" and (module, name) in get_option("pickler.safe.tuples"))
+ or (
+ opt == "deny"
+ and (module, name) not in get_option("pickler.unsafe.tuples")
+ )
+ ):
+ return super().find_class(module, name)
+ # Forbid everything else.
+ raise pkl.UnpicklingError(f"global '{module} . {name}' is forbidden")
Unpickler.dispatch = copy.copy(Unpickler.dispatch)
diff --git a/pandas/core/config_init.py b/pandas/core/config_init.py
index a8b63f97141c2..c202e5f8fe295 100644
--- a/pandas/core/config_init.py
+++ b/pandas/core/config_init.py
@@ -14,6 +14,8 @@
import os
from typing import Callable
+import yaml
+
import pandas._config.config as cf
from pandas._config.config import (
is_bool,
@@ -901,7 +903,6 @@ def register_converter_cb(key) -> None:
validator=is_instance_factory([type(None), str]),
)
-
with cf.config_prefix("future"):
cf.register_option(
"infer_string",
@@ -922,3 +923,78 @@ def register_converter_cb(key) -> None:
"(at which point this option will be deprecated).",
validator=is_one_of_factory([True, False]),
)
+
+# ------
+# Pickler
+# ------
+
+pickler_unpickle_mode = """
+: str
+ Determine which mode to use in {"off", "permit", "deny"}.
+"""
+
+pickler_safe_tuples = """
+: array
+ Used when pickler.unpickle.mode is "permit"
+ Array of safe tuples, e.g. [("builtins", "range"), ("builtins", "complex")]
+"""
+
+pickler_unsafe_tuples = """
+: array
+ Used when pickler.unpickle.mode is "deny"
+ Array of unsafe tuples, e.g. [("os", "system"), ("joblib", "load")]
+"""
+
+# location of config file from env var
+str_loc = os.environ.get("PANDAS_UNPICKLE_SECURE", "pickle_config.yml")
+safe_tuples = []
+unsafe_tuples = []
+
+if os.path.exists(str_loc):
+ pickle_config = yaml.load(open(str_loc), Loader=yaml.SafeLoader)
+
+ if pickle_config["mode"] == "permit":
+ for k, v in pickle_config["permit"].items():
+ for i in v:
+ safe_tuples.append((k, i))
+ elif pickle_config["mode"] == "deny":
+ for k, v in pickle_config["deny"].items():
+ for i in v:
+ unsafe_tuples.append((k, i))
+else:
+ pickle_config = {}
+ pickle_config["mode"] = "deny"
+ # see deny list example at https://pythonmana.com/2022/143/202205231222219535.html
+ unsafe_tuples = [
+ ("os", "system"),
+ ("posix", "system"),
+ ("builtins", "eval"),
+ ("builtins", "exec"),
+ ("builtins", "execfile"),
+ ("builtins", "compile"),
+ ("builtins", "open"),
+ ("builtins", "import"),
+ ("builtins", "__import__"),
+ ("builtins", "exit"),
+ ]
+
+with cf.config_prefix("pickler"):
+ cf.register_option(
+ "unpickle.mode",
+ # get the default value from the config file
+ pickle_config["mode"],
+ pickler_unpickle_mode,
+ validator=is_one_of_factory(["off", "permit", "deny"]),
+ )
+
+ cf.register_option(
+ "safe.tuples",
+ safe_tuples,
+ pickler_safe_tuples,
+ )
+
+ cf.register_option(
+ "unsafe.tuples",
+ unsafe_tuples,
+ pickler_unsafe_tuples,
+ )
\ No newline at end of file
diff --git a/pandas/io/pickle.py b/pandas/io/pickle.py
index 0dae0e7106b69..53c7f633362c3 100644
--- a/pandas/io/pickle.py
+++ b/pandas/io/pickle.py
@@ -8,6 +8,8 @@
)
import warnings
+from pandas._config import get_option
+
from pandas.compat import pickle_compat as pc
from pandas.util._decorators import doc
@@ -121,12 +123,15 @@ def read_pickle(
storage_options: StorageOptions | None = None,
) -> DataFrame | Series:
"""
- Load pickled pandas object (or any object) from file.
+ Load pickled pandas object (or any object) from file. By default, only a
+ safe subset of classes from builtins can be called while loading the
+ object. See http://guide/ for customizing the security settings.
.. warning::
Loading pickled data received from untrusted sources can be
- unsafe. See `here <https://docs.python.org/3/library/pickle.html>`__.
+ unsafe if not using the default security settings.
+ See `here <https://docs.python.org/3/library/pickle.html>`__.
Parameters
----------
@@ -181,6 +186,27 @@ def read_pickle(
3 3 8
4 4 9
"""
+
+ class RestrictedUnpickler(pickle.Unpickler):
+ def find_class(self, module, name):
+ opt = get_option("pickler.unpickle.mode")
+ # Only allow safe modules and classes. Tuples defined in config
+ # Do not allow unsafe modules and classes.
+ if (
+ (opt == "off")
+ or (
+ opt == "permit"
+ and (module, name) in get_option("pickler.safe.tuples")
+ )
+ or (
+ opt == "deny"
+ and (module, name) not in get_option("pickler.unsafe.tuples")
+ )
+ ):
+ return super().find_class(module, name)
+ # Forbid everything else.
+ raise pickle.UnpicklingError(f"global '{module} . {name}' is forbidden")
+
excs_to_catch = (AttributeError, ImportError, ModuleNotFoundError, TypeError)
with get_handle(
filepath_or_buffer,
@@ -199,7 +225,7 @@ def read_pickle(
with warnings.catch_warnings(record=True):
# We want to silence any warnings about, e.g. moved modules.
warnings.simplefilter("ignore", Warning)
- return pickle.load(handles.handle)
+ return RestrictedUnpickler.load(handles.handle)
except excs_to_catch:
# e.g.
# "No module named 'pandas.core.sparse.series'"
diff --git a/pandas/tests/io/data/pickle/test_forbidden.pkl b/pandas/tests/io/data/pickle/test_forbidden.pkl
new file mode 100644
index 0000000000000..5d5f134eec7cf
Binary files /dev/null and b/pandas/tests/io/data/pickle/test_forbidden.pkl differ
diff --git a/pandas/tests/io/test_clipboard.py b/pandas/tests/io/test_clipboard.py
index 3c0208fcc74ec..0cbb3e54d4a14 100644
--- a/pandas/tests/io/test_clipboard.py
+++ b/pandas/tests/io/test_clipboard.py
@@ -342,6 +342,13 @@ def test_invalid_encoding(self, df):
@pytest.mark.parametrize("data", ["\U0001f44d...", "Ωœ∑`...", "abcd..."])
def test_raw_roundtrip(self, data):
# PR #25040 wide unicode wasn't copied correctly on PY3 on windows
+ # adding coverage for when not implemented
+ try:
+ clipboard_set(data)
+ assert data == clipboard_get()
+ except PyperclipException:
+ with pytest.raises(PyperclipException, match=r".*not-implemented-error.*"):
+ clipboard_set(data)
df = DataFrame({"data": [data]})
df.to_clipboard()
result = read_clipboard()
@@ -420,4 +427,4 @@ def test_to_clipboard_pos_args_deprecation(self):
r"will be keyword-only."
)
with tm.assert_produces_warning(FutureWarning, match=msg):
- df.to_clipboard(True, None)
+ df.to_clipboard(True, None)
\ No newline at end of file
diff --git a/pandas/tests/io/test_pickle.py b/pandas/tests/io/test_pickle.py
index 4f3993a038197..0ee7e6681ec42 100644
--- a/pandas/tests/io/test_pickle.py
+++ b/pandas/tests/io/test_pickle.py
@@ -639,7 +639,24 @@ def test_pickle_frame_v124_unpickle_130(datapath):
expected = DataFrame(index=[], columns=[])
tm.assert_frame_equal(df, expected)
+
+def test_read_pickle_forbidden():
+ # related to CVE - https://nvd.nist.gov/vuln/detail/CVE-2020-13091
+ class MyEvilPickle:
+ def __reduce__(self):
+ return (os.system, ("whoami",))
+
+ pickle_data = pickle.dumps(MyEvilPickle())
+ # storing the serialized output into a file in current directory
+ path = os.path.join(os.path.dirname(__file__), "data", "pickle", "test_forbidden.pkl")
+ with open(path, "wb") as file:
+ file.write(pickle_data)
+
+ with pytest.raises(pickle.UnpicklingError, match=r".* forbidden"):
+ pd.read_pickle(path)
+
+
def test_pickle_pos_args_deprecation():
# GH-54229
df = DataFrame({"a": [1, 2, 3]})
@@ -649,4 +666,4 @@ def test_pickle_pos_args_deprecation():
)
with tm.assert_produces_warning(FutureWarning, match=msg):
buffer = io.BytesIO()
- df.to_pickle(buffer, "infer")
+ df.to_pickle(buffer, "infer")
\ No newline at end of file
| - [ ] closes #xxxx (Replace xxxx with the GitHub issue number)
- [ ] [Tests added and passed](https://pandas.pydata.org/pandas-docs/dev/development/contributing_codebase.html#writing-tests) if fixing a bug or adding a new feature
- [ ] All [code checks passed](https://pandas.pydata.org/pandas-docs/dev/development/contributing_codebase.html#pre-commit).
- [ ] Added [type annotations](https://pandas.pydata.org/pandas-docs/dev/development/contributing_codebase.html#type-hints) to new arguments/methods/functions.
- [ ] Added an entry in the latest `doc/source/whatsnew/vX.X.X.rst` file if fixing a bug or adding a new feature.
| https://api.github.com/repos/pandas-dev/pandas/pulls/57094 | 2024-01-26T19:24:53Z | 2024-01-26T19:49:11Z | null | 2024-01-26T19:53:40Z |
Remove support for `py.path` objects in IO readers | diff --git a/doc/source/conf.py b/doc/source/conf.py
index be6150d4e54ba..cb7352341eedd 100644
--- a/doc/source/conf.py
+++ b/doc/source/conf.py
@@ -460,7 +460,6 @@
"dateutil": ("https://dateutil.readthedocs.io/en/latest/", None),
"matplotlib": ("https://matplotlib.org/stable/", None),
"numpy": ("https://numpy.org/doc/stable/", None),
- "py": ("https://pylib.readthedocs.io/en/latest/", None),
"python": ("https://docs.python.org/3/", None),
"scipy": ("https://docs.scipy.org/doc/scipy/", None),
"pyarrow": ("https://arrow.apache.org/docs/", None),
diff --git a/doc/source/user_guide/io.rst b/doc/source/user_guide/io.rst
index bb5b4e056d527..8adbb53227586 100644
--- a/doc/source/user_guide/io.rst
+++ b/doc/source/user_guide/io.rst
@@ -61,8 +61,8 @@ Basic
+++++
filepath_or_buffer : various
- Either a path to a file (a :class:`python:str`, :class:`python:pathlib.Path`,
- or :class:`py:py._path.local.LocalPath`), URL (including http, ftp, and S3
+ Either a path to a file (a :class:`python:str`, :class:`python:pathlib.Path`)
+ URL (including http, ftp, and S3
locations), or any object with a ``read()`` method (such as an open file or
:class:`~python:io.StringIO`).
sep : str, defaults to ``','`` for :func:`read_csv`, ``\t`` for :func:`read_table`
diff --git a/doc/source/whatsnew/v3.0.0.rst b/doc/source/whatsnew/v3.0.0.rst
index 950082f9281c5..73201fa93a8aa 100644
--- a/doc/source/whatsnew/v3.0.0.rst
+++ b/doc/source/whatsnew/v3.0.0.rst
@@ -84,7 +84,7 @@ See :ref:`install.dependencies` and :ref:`install.optional_dependencies` for mor
Other API changes
^^^^^^^^^^^^^^^^^
--
+- 3rd party ``py.path`` objects are no longer explicitly supported in IO methods. Use :py:class:`pathlib.Path` objects instead (:issue:`57091`)
-
.. ---------------------------------------------------------------------------
diff --git a/pandas/_testing/__init__.py b/pandas/_testing/__init__.py
index 672c16a85086c..3f5fd2e61b0cb 100644
--- a/pandas/_testing/__init__.py
+++ b/pandas/_testing/__init__.py
@@ -34,7 +34,6 @@
Series,
)
from pandas._testing._io import (
- round_trip_localpath,
round_trip_pathlib,
round_trip_pickle,
write_to_compressed,
@@ -609,7 +608,6 @@ def shares_memory(left, right) -> bool:
"OBJECT_DTYPES",
"raise_assert_detail",
"raises_chained_assignment_error",
- "round_trip_localpath",
"round_trip_pathlib",
"round_trip_pickle",
"setitem",
diff --git a/pandas/_testing/_io.py b/pandas/_testing/_io.py
index 95977edb600ad..2955108d3db1a 100644
--- a/pandas/_testing/_io.py
+++ b/pandas/_testing/_io.py
@@ -89,35 +89,6 @@ def round_trip_pathlib(writer, reader, path: str | None = None):
return obj
-def round_trip_localpath(writer, reader, path: str | None = None):
- """
- Write an object to file specified by a py.path LocalPath and read it back.
-
- Parameters
- ----------
- writer : callable bound to pandas object
- IO writing function (e.g. DataFrame.to_csv )
- reader : callable
- IO reading function (e.g. pd.read_csv )
- path : str, default None
- The path where the object is written and then read.
-
- Returns
- -------
- pandas object
- The original object that was serialized and then re-read.
- """
- import pytest
-
- LocalPath = pytest.importorskip("py.path").local
- if path is None:
- path = "___localpath___"
- with ensure_clean(path) as path:
- writer(LocalPath(path))
- obj = reader(LocalPath(path))
- return obj
-
-
def write_to_compressed(compression, path, data, dest: str = "test") -> None:
"""
Write data to a compressed file.
diff --git a/pandas/io/common.py b/pandas/io/common.py
index 576bf7215f363..16d7cb76f9ce9 100644
--- a/pandas/io/common.py
+++ b/pandas/io/common.py
@@ -318,7 +318,7 @@ def _get_filepath_or_buffer(
Parameters
----------
- filepath_or_buffer : a url, filepath (str, py.path.local or pathlib.Path),
+ filepath_or_buffer : a url, filepath (str or pathlib.Path),
or buffer
{compression_options}
diff --git a/pandas/io/excel/_base.py b/pandas/io/excel/_base.py
index 2189f54263dec..c7642236d4b2a 100644
--- a/pandas/io/excel/_base.py
+++ b/pandas/io/excel/_base.py
@@ -1441,7 +1441,7 @@ class ExcelFile:
Parameters
----------
- path_or_buffer : str, bytes, path object (pathlib.Path or py._path.local.LocalPath),
+ path_or_buffer : str, bytes, pathlib.Path,
A file-like object, xlrd workbook or openpyxl workbook.
If a string or path object, expected to be a path to a
.xls, .xlsx, .xlsb, .xlsm, .odf, .ods, or .odt file.
diff --git a/pandas/io/stata.py b/pandas/io/stata.py
index 576e27f202524..2ac49600d4d7e 100644
--- a/pandas/io/stata.py
+++ b/pandas/io/stata.py
@@ -216,7 +216,7 @@
Parameters
----------
path_or_buf : path (string), buffer or path object
- string, path object (pathlib.Path or py._path.local.LocalPath) or object
+ string, pathlib.Path or object
implementing a binary read() functions.
{_statafile_processing_params1}
{_statafile_processing_params2}
@@ -2258,7 +2258,7 @@ class StataWriter(StataParser):
Parameters
----------
fname : path (string), buffer or path object
- string, path object (pathlib.Path or py._path.local.LocalPath) or
+ string, pathlib.Path or
object implementing a binary write() functions. If using a buffer
then the buffer will not be automatically closed after the file
is written.
@@ -3208,7 +3208,7 @@ class StataWriter117(StataWriter):
Parameters
----------
fname : path (string), buffer or path object
- string, path object (pathlib.Path or py._path.local.LocalPath) or
+ string, pathlib.Path or
object implementing a binary write() functions. If using a buffer
then the buffer will not be automatically closed after the file
is written.
@@ -3594,7 +3594,7 @@ class StataWriterUTF8(StataWriter117):
Parameters
----------
fname : path (string), buffer or path object
- string, path object (pathlib.Path or py._path.local.LocalPath) or
+ string, pathlib.Path or
object implementing a binary write() functions. If using a buffer
then the buffer will not be automatically closed after the file
is written.
diff --git a/pandas/tests/io/excel/test_readers.py b/pandas/tests/io/excel/test_readers.py
index 04a25317c8017..708f01839a23c 100644
--- a/pandas/tests/io/excel/test_readers.py
+++ b/pandas/tests/io/excel/test_readers.py
@@ -966,19 +966,6 @@ def test_read_from_pathlib_path(self, read_ext):
tm.assert_frame_equal(expected, actual)
- @td.skip_if_no("py.path")
- def test_read_from_py_localpath(self, read_ext):
- # GH12655
- from py.path import local as LocalPath
-
- str_path = os.path.join("test1" + read_ext)
- expected = pd.read_excel(str_path, sheet_name="Sheet1", index_col=0)
-
- path_obj = LocalPath().join("test1" + read_ext)
- actual = pd.read_excel(path_obj, sheet_name="Sheet1", index_col=0)
-
- tm.assert_frame_equal(expected, actual)
-
def test_close_from_py_localpath(self, read_ext):
# GH31467
str_path = os.path.join("test1" + read_ext)
diff --git a/pandas/tests/io/excel/test_writers.py b/pandas/tests/io/excel/test_writers.py
index 76a138a295bda..6aea7464ee8dc 100644
--- a/pandas/tests/io/excel/test_writers.py
+++ b/pandas/tests/io/excel/test_writers.py
@@ -1319,18 +1319,6 @@ def test_path_path_lib(self, engine, ext):
result = tm.round_trip_pathlib(writer, reader, path=f"foo{ext}")
tm.assert_frame_equal(result, df)
- def test_path_local_path(self, engine, ext):
- df = DataFrame(
- 1.1 * np.arange(120).reshape((30, 4)),
- columns=Index(list("ABCD")),
- index=Index([f"i-{i}" for i in range(30)]),
- )
- writer = partial(df.to_excel, engine=engine)
-
- reader = partial(pd.read_excel, index_col=0)
- result = tm.round_trip_localpath(writer, reader, path=f"foo{ext}")
- tm.assert_frame_equal(result, df)
-
def test_merged_cell_custom_objects(self, path):
# see GH-27006
mi = MultiIndex.from_tuples(
diff --git a/pandas/tests/io/parser/common/test_file_buffer_url.py b/pandas/tests/io/parser/common/test_file_buffer_url.py
index 5e31b4c6b644d..774f6b61df517 100644
--- a/pandas/tests/io/parser/common/test_file_buffer_url.py
+++ b/pandas/tests/io/parser/common/test_file_buffer_url.py
@@ -79,20 +79,6 @@ def test_path_path_lib(all_parsers):
tm.assert_frame_equal(df, result)
-@xfail_pyarrow # AssertionError: DataFrame.index are different
-def test_path_local_path(all_parsers):
- parser = all_parsers
- df = DataFrame(
- 1.1 * np.arange(120).reshape((30, 4)),
- columns=Index(list("ABCD"), dtype=object),
- index=Index([f"i-{i}" for i in range(30)], dtype=object),
- )
- result = tm.round_trip_localpath(
- df.to_csv, lambda p: parser.read_csv(p, index_col=0)
- )
- tm.assert_frame_equal(df, result)
-
-
def test_nonexistent_path(all_parsers):
# gh-2428: pls no segfault
# gh-14086: raise more helpful FileNotFoundError
diff --git a/pandas/tests/io/pytables/test_read.py b/pandas/tests/io/pytables/test_read.py
index e4a3ea1fc9db8..c8563ee4af4a8 100644
--- a/pandas/tests/io/pytables/test_read.py
+++ b/pandas/tests/io/pytables/test_read.py
@@ -22,7 +22,6 @@
_maybe_remove,
ensure_clean_store,
)
-from pandas.util import _test_decorators as td
from pandas.io.pytables import TableIterator
@@ -337,25 +336,6 @@ def test_read_from_pathlib_path(tmp_path, setup_path):
tm.assert_frame_equal(expected, actual)
-@td.skip_if_no("py.path")
-def test_read_from_py_localpath(tmp_path, setup_path):
- # GH11773
- from py.path import local as LocalPath
-
- expected = DataFrame(
- np.random.default_rng(2).random((4, 5)),
- index=list("abcd"),
- columns=list("ABCDE"),
- )
- filename = tmp_path / setup_path
- path_obj = LocalPath(filename)
-
- expected.to_hdf(path_obj, key="df", mode="a")
- actual = read_hdf(path_obj, key="df")
-
- tm.assert_frame_equal(expected, actual)
-
-
@pytest.mark.parametrize("format", ["fixed", "table"])
def test_read_hdf_series_mode_r(tmp_path, format, setup_path):
# GH 16583
diff --git a/pandas/tests/io/pytables/test_store.py b/pandas/tests/io/pytables/test_store.py
index 82d3052e7f5d6..de56ab614dcd4 100644
--- a/pandas/tests/io/pytables/test_store.py
+++ b/pandas/tests/io/pytables/test_store.py
@@ -958,25 +958,6 @@ def test_pickle_path_localpath():
tm.assert_frame_equal(df, result)
-def test_path_localpath_hdfstore():
- df = DataFrame(
- 1.1 * np.arange(120).reshape((30, 4)),
- columns=Index(list("ABCD"), dtype=object),
- index=Index([f"i-{i}" for i in range(30)], dtype=object),
- )
-
- def writer(path):
- with HDFStore(path) as store:
- df.to_hdf(store, key="df")
-
- def reader(path):
- with HDFStore(path) as store:
- return read_hdf(store, "df")
-
- result = tm.round_trip_localpath(writer, reader)
- tm.assert_frame_equal(df, result)
-
-
@pytest.mark.parametrize("propindexes", [True, False])
def test_copy(propindexes):
df = DataFrame(
diff --git a/pandas/tests/io/sas/test_sas7bdat.py b/pandas/tests/io/sas/test_sas7bdat.py
index b71896c77ffb5..889ef61740a2c 100644
--- a/pandas/tests/io/sas/test_sas7bdat.py
+++ b/pandas/tests/io/sas/test_sas7bdat.py
@@ -9,7 +9,6 @@
from pandas.compat import IS64
from pandas.errors import EmptyDataError
-import pandas.util._test_decorators as td
import pandas as pd
import pandas._testing as tm
@@ -82,17 +81,6 @@ def test_path_pathlib(self, dirpath, data_test_ix):
df = pd.read_sas(fname, encoding="utf-8")
tm.assert_frame_equal(df, expected)
- @td.skip_if_no("py.path")
- @pytest.mark.slow
- def test_path_localpath(self, dirpath, data_test_ix):
- from py.path import local as LocalPath
-
- expected, test_ix = data_test_ix
- for k in test_ix:
- fname = LocalPath(os.path.join(dirpath, f"test{k}.sas7bdat"))
- df = pd.read_sas(fname, encoding="utf-8")
- tm.assert_frame_equal(df, expected)
-
@pytest.mark.slow
@pytest.mark.parametrize("chunksize", (3, 5, 10, 11))
@pytest.mark.parametrize("k", range(1, 17))
diff --git a/pandas/tests/io/test_common.py b/pandas/tests/io/test_common.py
index 074033868635a..b43f430b8895b 100644
--- a/pandas/tests/io/test_common.py
+++ b/pandas/tests/io/test_common.py
@@ -19,7 +19,6 @@
import pytest
from pandas.compat import is_platform_windows
-import pandas.util._test_decorators as td
import pandas as pd
import pandas._testing as tm
@@ -41,16 +40,6 @@ def __fspath__(self):
return self.path
-# Functions that consume a string path and return a string or path-like object
-path_types = [str, CustomFSPath, Path]
-
-try:
- from py.path import local as LocalPath
-
- path_types.append(LocalPath)
-except ImportError:
- pass
-
HERE = os.path.abspath(os.path.dirname(__file__))
@@ -86,13 +75,6 @@ def test_stringify_path_pathlib(self):
redundant_path = icom.stringify_path(Path("foo//bar"))
assert redundant_path == os.path.join("foo", "bar")
- @td.skip_if_no("py.path")
- def test_stringify_path_localpath(self):
- path = os.path.join("foo", "bar")
- abs_path = os.path.abspath(path)
- lpath = LocalPath(path)
- assert icom.stringify_path(lpath) == abs_path
-
def test_stringify_path_fspath(self):
p = CustomFSPath("foo/bar.csv")
result = icom.stringify_path(p)
@@ -105,7 +87,7 @@ def test_stringify_file_and_path_like(self):
with fsspec.open(f"file://{path}", mode="wb") as fsspec_obj:
assert fsspec_obj == icom.stringify_path(fsspec_obj)
- @pytest.mark.parametrize("path_type", path_types)
+ @pytest.mark.parametrize("path_type", [str, CustomFSPath, Path])
def test_infer_compression_from_path(self, compression_format, path_type):
extension, expected = compression_format
path = path_type("foo/bar.csv" + extension)
@@ -114,7 +96,6 @@ def test_infer_compression_from_path(self, compression_format, path_type):
@pytest.mark.parametrize("path_type", [str, CustomFSPath, Path])
def test_get_handle_with_path(self, path_type):
- # ignore LocalPath: it creates strange paths: /absolute/~/sometest
with tempfile.TemporaryDirectory(dir=Path.home()) as tmp:
filename = path_type("~/" + Path(tmp).name + "/sometest")
with icom.get_handle(filename, "w") as handles:
diff --git a/pandas/tests/io/test_feather.py b/pandas/tests/io/test_feather.py
index 22a7d3b83a459..f012fcadc5592 100644
--- a/pandas/tests/io/test_feather.py
+++ b/pandas/tests/io/test_feather.py
@@ -140,15 +140,6 @@ def test_path_pathlib(self):
result = tm.round_trip_pathlib(df.to_feather, read_feather)
tm.assert_frame_equal(df, result)
- def test_path_localpath(self):
- df = pd.DataFrame(
- 1.1 * np.arange(120).reshape((30, 4)),
- columns=pd.Index(list("ABCD"), dtype=object),
- index=pd.Index([f"i-{i}" for i in range(30)], dtype=object),
- ).reset_index()
- result = tm.round_trip_localpath(df.to_feather, read_feather)
- tm.assert_frame_equal(df, result)
-
def test_passthrough_keywords(self):
df = pd.DataFrame(
1.1 * np.arange(120).reshape((30, 4)),
diff --git a/pandas/tests/io/test_pickle.py b/pandas/tests/io/test_pickle.py
index f6c7f66abe5d3..57c7829924531 100644
--- a/pandas/tests/io/test_pickle.py
+++ b/pandas/tests/io/test_pickle.py
@@ -229,16 +229,6 @@ def test_pickle_path_pathlib():
tm.assert_frame_equal(df, result)
-def test_pickle_path_localpath():
- df = DataFrame(
- 1.1 * np.arange(120).reshape((30, 4)),
- columns=Index(list("ABCD"), dtype=object),
- index=Index([f"i-{i}" for i in range(30)], dtype=object),
- )
- result = tm.round_trip_localpath(df.to_pickle, pd.read_pickle)
- tm.assert_frame_equal(df, result)
-
-
# ---------------------
# test pickle compression
# ---------------------
diff --git a/pandas/tests/io/test_stata.py b/pandas/tests/io/test_stata.py
index 5c6377349304c..4aa0b2f5ae8c9 100644
--- a/pandas/tests/io/test_stata.py
+++ b/pandas/tests/io/test_stata.py
@@ -1546,17 +1546,6 @@ def test_path_pathlib(self):
result = tm.round_trip_pathlib(df.to_stata, reader)
tm.assert_frame_equal(df, result)
- def test_pickle_path_localpath(self):
- df = DataFrame(
- 1.1 * np.arange(120).reshape((30, 4)),
- columns=pd.Index(list("ABCD"), dtype=object),
- index=pd.Index([f"i-{i}" for i in range(30)], dtype=object),
- )
- df.index.name = "index"
- reader = lambda x: read_stata(x).set_index("index")
- result = tm.round_trip_localpath(df.to_stata, reader)
- tm.assert_frame_equal(df, result)
-
@pytest.mark.parametrize("write_index", [True, False])
def test_value_labels_iterator(self, write_index):
# GH 16923
| This library is in maintenance mode and recommends `pathlib` objects so removing support for this object in IO methods.
https://py.readthedocs.io/en/latest/path.html#py-path | https://api.github.com/repos/pandas-dev/pandas/pulls/57091 | 2024-01-26T18:51:18Z | 2024-01-26T22:35:27Z | 2024-01-26T22:35:27Z | 2024-01-26T22:35:30Z |
Backport PR #57084 on branch 2.2.x (Fix mem leak in read_csv) | diff --git a/asv_bench/benchmarks/io/csv.py b/asv_bench/benchmarks/io/csv.py
index 9ac83db4f85b9..dae6107db4d92 100644
--- a/asv_bench/benchmarks/io/csv.py
+++ b/asv_bench/benchmarks/io/csv.py
@@ -408,6 +408,9 @@ def time_read_stringcsv(self, engine):
def time_read_bytescsv(self, engine):
read_csv(self.data(self.BytesIO_input), engine=engine)
+ def peakmem_read_csv(self, engine):
+ read_csv(self.data(self.BytesIO_input), engine=engine)
+
class ReadCSVCategorical(BaseIO):
fname = "__test__.csv"
diff --git a/doc/source/whatsnew/v2.2.1.rst b/doc/source/whatsnew/v2.2.1.rst
index 23da4a7f6ab25..b9b2821ebc468 100644
--- a/doc/source/whatsnew/v2.2.1.rst
+++ b/doc/source/whatsnew/v2.2.1.rst
@@ -13,6 +13,7 @@ including other versions of pandas.
Fixed regressions
~~~~~~~~~~~~~~~~~
+- Fixed memory leak in :func:`read_csv` (:issue:`57039`)
- Fixed performance regression in :meth:`Series.combine_first` (:issue:`55845`)
- Fixed regression in :func:`merge_ordered` raising ``TypeError`` for ``fill_method="ffill"`` and ``how="left"`` (:issue:`57010`)
- Fixed regression in :meth:`DataFrameGroupBy.idxmin`, :meth:`DataFrameGroupBy.idxmax`, :meth:`SeriesGroupBy.idxmin`, :meth:`SeriesGroupBy.idxmax` ignoring the ``skipna`` argument (:issue:`57040`)
diff --git a/pandas/_libs/src/parser/tokenizer.c b/pandas/_libs/src/parser/tokenizer.c
index 0e4188bea4dc7..c9f7a796a9b1c 100644
--- a/pandas/_libs/src/parser/tokenizer.c
+++ b/pandas/_libs/src/parser/tokenizer.c
@@ -109,6 +109,14 @@ void parser_set_default_options(parser_t *self) {
parser_t *parser_new(void) { return (parser_t *)calloc(1, sizeof(parser_t)); }
+static void parser_clear_data_buffers(parser_t *self) {
+ free_if_not_null((void *)&self->stream);
+ free_if_not_null((void *)&self->words);
+ free_if_not_null((void *)&self->word_starts);
+ free_if_not_null((void *)&self->line_start);
+ free_if_not_null((void *)&self->line_fields);
+}
+
static void parser_cleanup(parser_t *self) {
// XXX where to put this
free_if_not_null((void *)&self->error_msg);
@@ -119,6 +127,7 @@ static void parser_cleanup(parser_t *self) {
self->skipset = NULL;
}
+ parser_clear_data_buffers(self);
if (self->cb_cleanup != NULL) {
self->cb_cleanup(self->source);
self->cb_cleanup = NULL;
| Backport PR #57084: Fix mem leak in read_csv | https://api.github.com/repos/pandas-dev/pandas/pulls/57090 | 2024-01-26T18:42:06Z | 2024-01-26T19:50:09Z | 2024-01-26T19:50:09Z | 2024-01-26T19:50:09Z |
BUG: wide_to_long with string columns | diff --git a/doc/source/whatsnew/v2.2.1.rst b/doc/source/whatsnew/v2.2.1.rst
index 23da4a7f6ab25..5d60eae8f5ed0 100644
--- a/doc/source/whatsnew/v2.2.1.rst
+++ b/doc/source/whatsnew/v2.2.1.rst
@@ -15,6 +15,7 @@ Fixed regressions
~~~~~~~~~~~~~~~~~
- Fixed performance regression in :meth:`Series.combine_first` (:issue:`55845`)
- Fixed regression in :func:`merge_ordered` raising ``TypeError`` for ``fill_method="ffill"`` and ``how="left"`` (:issue:`57010`)
+- Fixed regression in :func:`wide_to_long` raising an ``AttributeError`` for string columns (:issue:`57066`)
- Fixed regression in :meth:`DataFrameGroupBy.idxmin`, :meth:`DataFrameGroupBy.idxmax`, :meth:`SeriesGroupBy.idxmin`, :meth:`SeriesGroupBy.idxmax` ignoring the ``skipna`` argument (:issue:`57040`)
- Fixed regression in :meth:`DataFrameGroupBy.idxmin`, :meth:`DataFrameGroupBy.idxmax`, :meth:`SeriesGroupBy.idxmin`, :meth:`SeriesGroupBy.idxmax` where values containing the minimum or maximum value for the dtype could produce incorrect results (:issue:`57040`)
- Fixed regression in :meth:`Series.pct_change` raising a ``ValueError`` for an empty :class:`Series` (:issue:`57056`)
diff --git a/pandas/core/reshape/melt.py b/pandas/core/reshape/melt.py
index 1ae7000f56bc9..4bf1742a83c66 100644
--- a/pandas/core/reshape/melt.py
+++ b/pandas/core/reshape/melt.py
@@ -458,8 +458,7 @@ def wide_to_long(
def get_var_names(df, stub: str, sep: str, suffix: str):
regex = rf"^{re.escape(stub)}{re.escape(sep)}{suffix}$"
- pattern = re.compile(regex)
- return df.columns[df.columns.str.match(pattern)]
+ return df.columns[df.columns.str.match(regex)]
def melt_stub(df, stub: str, i, j, value_vars, sep: str):
newdf = melt(
diff --git a/pandas/core/strings/accessor.py b/pandas/core/strings/accessor.py
index f0ad5b662de84..804beb44bd699 100644
--- a/pandas/core/strings/accessor.py
+++ b/pandas/core/strings/accessor.py
@@ -1332,14 +1332,14 @@ def contains(
return self._wrap_result(result, fill_value=na, returns_string=False)
@forbid_nonstring_types(["bytes"])
- def match(self, pat, case: bool = True, flags: int = 0, na=None):
+ def match(self, pat: str, case: bool = True, flags: int = 0, na=None):
"""
Determine if each string starts with a match of a regular expression.
Parameters
----------
pat : str
- Character sequence or regular expression.
+ Character sequence.
case : bool, default True
If True, case sensitive.
flags : int, default 0 (no flags)
diff --git a/pandas/tests/reshape/test_melt.py b/pandas/tests/reshape/test_melt.py
index eeba7f441d75a..f224a45ca3279 100644
--- a/pandas/tests/reshape/test_melt.py
+++ b/pandas/tests/reshape/test_melt.py
@@ -1217,3 +1217,33 @@ def test_missing_stubname(self, dtype):
new_level = expected.index.levels[0].astype(dtype)
expected.index = expected.index.set_levels(new_level, level=0)
tm.assert_frame_equal(result, expected)
+
+
+def test_wide_to_long_pyarrow_string_columns():
+ # GH 57066
+ pytest.importorskip("pyarrow")
+ df = DataFrame(
+ {
+ "ID": {0: 1},
+ "R_test1": {0: 1},
+ "R_test2": {0: 1},
+ "R_test3": {0: 2},
+ "D": {0: 1},
+ }
+ )
+ df.columns = df.columns.astype("string[pyarrow_numpy]")
+ result = wide_to_long(
+ df, stubnames="R", i="ID", j="UNPIVOTED", sep="_", suffix=".*"
+ )
+ expected = DataFrame(
+ [[1, 1], [1, 1], [1, 2]],
+ columns=Index(["D", "R"], dtype=object),
+ index=pd.MultiIndex.from_arrays(
+ [
+ [1, 1, 1],
+ Index(["test1", "test2", "test3"], dtype="string[pyarrow_numpy]"),
+ ],
+ names=["ID", "UNPIVOTED"],
+ ),
+ )
+ tm.assert_frame_equal(result, expected)
| - [ ] closes #57066 (Replace xxxx with the GitHub issue number)
- [ ] [Tests added and passed](https://pandas.pydata.org/pandas-docs/dev/development/contributing_codebase.html#writing-tests) if fixing a bug or adding a new feature
- [ ] All [code checks passed](https://pandas.pydata.org/pandas-docs/dev/development/contributing_codebase.html#pre-commit).
- [ ] Added [type annotations](https://pandas.pydata.org/pandas-docs/dev/development/contributing_codebase.html#type-hints) to new arguments/methods/functions.
- [ ] Added an entry in the latest `doc/source/whatsnew/vX.X.X.rst` file if fixing a bug or adding a new feature.
Appears `str.match` never accepted a `re.Pattern` so updated the docs too
| https://api.github.com/repos/pandas-dev/pandas/pulls/57089 | 2024-01-26T18:37:00Z | 2024-01-28T23:54:39Z | 2024-01-28T23:54:39Z | 2024-01-29T03:42:44Z |
Backport PR #57046 on branch 2.2.x (REGR: groupby.idxmin/idxmax wrong result on extreme values) | diff --git a/doc/source/whatsnew/v2.2.1.rst b/doc/source/whatsnew/v2.2.1.rst
index 93965ffed23d3..23da4a7f6ab25 100644
--- a/doc/source/whatsnew/v2.2.1.rst
+++ b/doc/source/whatsnew/v2.2.1.rst
@@ -15,6 +15,8 @@ Fixed regressions
~~~~~~~~~~~~~~~~~
- Fixed performance regression in :meth:`Series.combine_first` (:issue:`55845`)
- Fixed regression in :func:`merge_ordered` raising ``TypeError`` for ``fill_method="ffill"`` and ``how="left"`` (:issue:`57010`)
+- Fixed regression in :meth:`DataFrameGroupBy.idxmin`, :meth:`DataFrameGroupBy.idxmax`, :meth:`SeriesGroupBy.idxmin`, :meth:`SeriesGroupBy.idxmax` ignoring the ``skipna`` argument (:issue:`57040`)
+- Fixed regression in :meth:`DataFrameGroupBy.idxmin`, :meth:`DataFrameGroupBy.idxmax`, :meth:`SeriesGroupBy.idxmin`, :meth:`SeriesGroupBy.idxmax` where values containing the minimum or maximum value for the dtype could produce incorrect results (:issue:`57040`)
- Fixed regression in :meth:`Series.pct_change` raising a ``ValueError`` for an empty :class:`Series` (:issue:`57056`)
.. ---------------------------------------------------------------------------
diff --git a/pandas/_libs/groupby.pyx b/pandas/_libs/groupby.pyx
index 19d71b0a6fde3..ac24c0bb8df88 100644
--- a/pandas/_libs/groupby.pyx
+++ b/pandas/_libs/groupby.pyx
@@ -1767,6 +1767,7 @@ def group_idxmin_idxmax(
Py_ssize_t i, j, N, K, lab
numeric_object_t val
numeric_object_t[:, ::1] group_min_or_max
+ uint8_t[:, ::1] seen
bint uses_mask = mask is not None
bint isna_entry
bint compute_max = name == "idxmax"
@@ -1780,13 +1781,10 @@ def group_idxmin_idxmax(
if numeric_object_t is object:
group_min_or_max = np.empty((<object>out).shape, dtype=object)
+ seen = np.zeros((<object>out).shape, dtype=np.uint8)
else:
group_min_or_max = np.empty_like(out, dtype=values.dtype)
- if N > 0 and K > 0:
- # When N or K is zero, we never use group_min_or_max
- group_min_or_max[:] = _get_min_or_max(
- values[0, 0], compute_max, is_datetimelike
- )
+ seen = np.zeros_like(out, dtype=np.uint8)
# When using transform, we need a valid value for take in the case
# a category is not observed; these values will be dropped
@@ -1802,6 +1800,7 @@ def group_idxmin_idxmax(
if not skipna and out[lab, j] == -1:
# Once we've hit NA there is no going back
continue
+
val = values[i, j]
if uses_mask:
@@ -1810,10 +1809,14 @@ def group_idxmin_idxmax(
isna_entry = _treat_as_na(val, is_datetimelike)
if isna_entry:
- if not skipna:
+ if not skipna or not seen[lab, j]:
out[lab, j] = -1
else:
- if compute_max:
+ if not seen[lab, j]:
+ seen[lab, j] = True
+ group_min_or_max[lab, j] = val
+ out[lab, j] = i
+ elif compute_max:
if val > group_min_or_max[lab, j]:
group_min_or_max[lab, j] = val
out[lab, j] = i
diff --git a/pandas/core/groupby/ops.py b/pandas/core/groupby/ops.py
index 5e83eaee02afc..e2ddf9aa5c0c1 100644
--- a/pandas/core/groupby/ops.py
+++ b/pandas/core/groupby/ops.py
@@ -424,6 +424,7 @@ def _call_cython_op(
mask=mask,
result_mask=result_mask,
is_datetimelike=is_datetimelike,
+ **kwargs,
)
elif self.how in ["sem", "std", "var", "ohlc", "prod", "median"]:
if self.how in ["std", "sem"]:
diff --git a/pandas/tests/groupby/test_reductions.py b/pandas/tests/groupby/test_reductions.py
index 425079f943aba..422322d03c4c0 100644
--- a/pandas/tests/groupby/test_reductions.py
+++ b/pandas/tests/groupby/test_reductions.py
@@ -195,6 +195,68 @@ def test_empty(frame_or_series, bool_agg_func):
tm.assert_equal(result, expected)
+@pytest.mark.parametrize("how", ["idxmin", "idxmax"])
+def test_idxmin_idxmax_extremes(how, any_real_numpy_dtype):
+ # GH#57040
+ if any_real_numpy_dtype is int or any_real_numpy_dtype is float:
+ # No need to test
+ return
+ info = np.iinfo if "int" in any_real_numpy_dtype else np.finfo
+ min_value = info(any_real_numpy_dtype).min
+ max_value = info(any_real_numpy_dtype).max
+ df = DataFrame(
+ {"a": [2, 1, 1, 2], "b": [min_value, max_value, max_value, min_value]},
+ dtype=any_real_numpy_dtype,
+ )
+ gb = df.groupby("a")
+ result = getattr(gb, how)()
+ expected = DataFrame(
+ {"b": [1, 0]}, index=pd.Index([1, 2], name="a", dtype=any_real_numpy_dtype)
+ )
+ tm.assert_frame_equal(result, expected)
+
+
+@pytest.mark.parametrize("how", ["idxmin", "idxmax"])
+def test_idxmin_idxmax_extremes_skipna(skipna, how, float_numpy_dtype):
+ # GH#57040
+ min_value = np.finfo(float_numpy_dtype).min
+ max_value = np.finfo(float_numpy_dtype).max
+ df = DataFrame(
+ {
+ "a": Series(np.repeat(range(1, 6), repeats=2), dtype="intp"),
+ "b": Series(
+ [
+ np.nan,
+ min_value,
+ np.nan,
+ max_value,
+ min_value,
+ np.nan,
+ max_value,
+ np.nan,
+ np.nan,
+ np.nan,
+ ],
+ dtype=float_numpy_dtype,
+ ),
+ },
+ )
+ gb = df.groupby("a")
+
+ warn = None if skipna else FutureWarning
+ msg = f"The behavior of DataFrameGroupBy.{how} with all-NA values"
+ with tm.assert_produces_warning(warn, match=msg):
+ result = getattr(gb, how)(skipna=skipna)
+ if skipna:
+ values = [1, 3, 4, 6, np.nan]
+ else:
+ values = np.nan
+ expected = DataFrame(
+ {"b": values}, index=pd.Index(range(1, 6), name="a", dtype="intp")
+ )
+ tm.assert_frame_equal(result, expected)
+
+
@pytest.mark.parametrize(
"func, values",
[
| Backport PR #57046: REGR: groupby.idxmin/idxmax wrong result on extreme values | https://api.github.com/repos/pandas-dev/pandas/pulls/57086 | 2024-01-26T17:20:17Z | 2024-01-26T18:41:39Z | 2024-01-26T18:41:39Z | 2024-01-26T18:41:39Z |
Fix mem leak in read_csv | diff --git a/asv_bench/benchmarks/io/csv.py b/asv_bench/benchmarks/io/csv.py
index 9ac83db4f85b9..dae6107db4d92 100644
--- a/asv_bench/benchmarks/io/csv.py
+++ b/asv_bench/benchmarks/io/csv.py
@@ -408,6 +408,9 @@ def time_read_stringcsv(self, engine):
def time_read_bytescsv(self, engine):
read_csv(self.data(self.BytesIO_input), engine=engine)
+ def peakmem_read_csv(self, engine):
+ read_csv(self.data(self.BytesIO_input), engine=engine)
+
class ReadCSVCategorical(BaseIO):
fname = "__test__.csv"
diff --git a/doc/source/whatsnew/v2.2.1.rst b/doc/source/whatsnew/v2.2.1.rst
index 93965ffed23d3..83ced0695ceb6 100644
--- a/doc/source/whatsnew/v2.2.1.rst
+++ b/doc/source/whatsnew/v2.2.1.rst
@@ -13,6 +13,7 @@ including other versions of pandas.
Fixed regressions
~~~~~~~~~~~~~~~~~
+- Fixed memory leak in :func:`read_csv` (:issue:`57039`)
- Fixed performance regression in :meth:`Series.combine_first` (:issue:`55845`)
- Fixed regression in :func:`merge_ordered` raising ``TypeError`` for ``fill_method="ffill"`` and ``how="left"`` (:issue:`57010`)
- Fixed regression in :meth:`Series.pct_change` raising a ``ValueError`` for an empty :class:`Series` (:issue:`57056`)
diff --git a/pandas/_libs/src/parser/tokenizer.c b/pandas/_libs/src/parser/tokenizer.c
index 0e4188bea4dc7..c9f7a796a9b1c 100644
--- a/pandas/_libs/src/parser/tokenizer.c
+++ b/pandas/_libs/src/parser/tokenizer.c
@@ -109,6 +109,14 @@ void parser_set_default_options(parser_t *self) {
parser_t *parser_new(void) { return (parser_t *)calloc(1, sizeof(parser_t)); }
+static void parser_clear_data_buffers(parser_t *self) {
+ free_if_not_null((void *)&self->stream);
+ free_if_not_null((void *)&self->words);
+ free_if_not_null((void *)&self->word_starts);
+ free_if_not_null((void *)&self->line_start);
+ free_if_not_null((void *)&self->line_fields);
+}
+
static void parser_cleanup(parser_t *self) {
// XXX where to put this
free_if_not_null((void *)&self->error_msg);
@@ -119,6 +127,7 @@ static void parser_cleanup(parser_t *self) {
self->skipset = NULL;
}
+ parser_clear_data_buffers(self);
if (self->cb_cleanup != NULL) {
self->cb_cleanup(self->source);
self->cb_cleanup = NULL;
| Pending validation but I believe this fixes #57039
Initially I misread this function was branching based off a return value that was always 0, but the actual value lies in the side effects | https://api.github.com/repos/pandas-dev/pandas/pulls/57084 | 2024-01-26T13:35:55Z | 2024-01-26T18:41:59Z | 2024-01-26T18:41:59Z | 2024-01-26T19:04:22Z |
Update install.rst to match the newest code | diff --git a/doc/source/getting_started/install.rst b/doc/source/getting_started/install.rst
index b54a220f74ca3..b2d137f12555e 100644
--- a/doc/source/getting_started/install.rst
+++ b/doc/source/getting_started/install.rst
@@ -277,11 +277,12 @@ Installable with ``pip install "pandas[excel]"``.
========================= ================== =============== =============================================================
Dependency Minimum Version pip extra Notes
========================= ================== =============== =============================================================
-xlrd 2.0.1 excel Reading Excel
-xlsxwriter 3.0.5 excel Writing Excel
-openpyxl 3.1.0 excel Reading / writing for xlsx files
+xlrd 2.0.1 excel Reading for xls files
+xlsxwriter 3.0.5 excel Writing for xlsx files
+openpyxl 3.1.0 excel Reading / writing for Excel 2010 xlsx/xlsm/xltx/xltm files
pyxlsb 1.0.10 excel Reading for xlsb files
-python-calamine 0.1.7 excel Reading for xls/xlsx/xlsb/ods files
+python-calamine 0.1.7 excel Reading for xls/xlsx/xlsm/xlsb/xla/xlam/ods files
+odfpy 1.4.1 excel Reading / writing for OpenDocument 1.2 files
========================= ================== =============== =============================================================
HTML
| enhancing the Excel files related documents of pandas | https://api.github.com/repos/pandas-dev/pandas/pulls/57081 | 2024-01-26T04:18:41Z | 2024-01-26T19:51:00Z | 2024-01-26T19:51:00Z | 2024-01-26T19:51:06Z |
BUG: np.matmul with Index raising TypeError | diff --git a/doc/source/whatsnew/v3.0.0.rst b/doc/source/whatsnew/v3.0.0.rst
index 950082f9281c5..7f2c90bc89a9a 100644
--- a/doc/source/whatsnew/v3.0.0.rst
+++ b/doc/source/whatsnew/v3.0.0.rst
@@ -142,7 +142,7 @@ Timezones
Numeric
^^^^^^^
--
+- Bug in ``np.matmul`` with :class:`Index` inputs raising a ``TypeError`` (:issue:`57079`)
-
Conversion
diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py
index bf20525e2c7fd..4b200c447cb4d 100644
--- a/pandas/core/indexes/base.py
+++ b/pandas/core/indexes/base.py
@@ -951,6 +951,9 @@ def __array_ufunc__(self, ufunc: np.ufunc, method: str_t, *inputs, **kwargs):
elif method == "reduce":
result = lib.item_from_zerodim(result)
return result
+ elif is_scalar(result):
+ # e.g. matmul
+ return result
if result.dtype == np.float16:
result = result.astype(np.float32)
diff --git a/pandas/tests/series/test_ufunc.py b/pandas/tests/series/test_ufunc.py
index 9d13ebf740eab..3ef319174313d 100644
--- a/pandas/tests/series/test_ufunc.py
+++ b/pandas/tests/series/test_ufunc.py
@@ -427,6 +427,13 @@ def test_np_matmul():
tm.assert_frame_equal(expected, result)
+@pytest.mark.parametrize("box", [pd.Index, pd.Series])
+def test_np_matmul_1D(box):
+ result = np.matmul(box([1, 2]), box([2, 3]))
+ assert result == 8
+ assert isinstance(result, np.int64)
+
+
def test_array_ufuncs_for_many_arguments():
# GH39853
def add3(x, y, z):
| null | https://api.github.com/repos/pandas-dev/pandas/pulls/57079 | 2024-01-26T01:46:49Z | 2024-01-29T00:01:46Z | 2024-01-29T00:01:46Z | 2024-01-29T03:42:29Z |
54628 fix find stack level memory leak | diff --git a/pandas/_testing/_warnings.py b/pandas/_testing/_warnings.py
index 731a6df776c4c..cff28f6a20472 100644
--- a/pandas/_testing/_warnings.py
+++ b/pandas/_testing/_warnings.py
@@ -220,7 +220,12 @@ def _assert_raised_with_correct_stacklevel(
frame = inspect.currentframe()
for _ in range(4):
frame = frame.f_back # type: ignore[union-attr]
- caller_filename = inspect.getfile(frame) # type: ignore[arg-type]
+ try:
+ caller_filename = inspect.getfile(frame) # type: ignore[arg-type]
+ finally:
+ # See note in
+ # https://docs.python.org/3/library/inspect.html#inspect.Traceback
+ del frame
msg = (
"Warning not set with correct stacklevel. "
f"File where warning is raised: {actual_warning.filename} != "
diff --git a/pandas/util/_exceptions.py b/pandas/util/_exceptions.py
index 573f76a63459b..5f50838d37315 100644
--- a/pandas/util/_exceptions.py
+++ b/pandas/util/_exceptions.py
@@ -9,6 +9,7 @@
if TYPE_CHECKING:
from collections.abc import Generator
+ from types import FrameType
@contextlib.contextmanager
@@ -42,15 +43,20 @@ def find_stack_level() -> int:
test_dir = os.path.join(pkg_dir, "tests")
# https://stackoverflow.com/questions/17407119/python-inspect-stack-is-slow
- frame = inspect.currentframe()
- n = 0
- while frame:
- fname = inspect.getfile(frame)
- if fname.startswith(pkg_dir) and not fname.startswith(test_dir):
- frame = frame.f_back
- n += 1
- else:
- break
+ frame: FrameType | None = inspect.currentframe()
+ try:
+ n = 0
+ while frame:
+ filename = inspect.getfile(frame)
+ if filename.startswith(pkg_dir) and not filename.startswith(test_dir):
+ frame = frame.f_back
+ n += 1
+ else:
+ break
+ finally:
+ # See note in
+ # https://docs.python.org/3/library/inspect.html#inspect.Traceback
+ del frame
return n
| - [ ] closes #54628 (Replace xxxx with the GitHub issue number)
- [ ] [Tests added and passed](https://pandas.pydata.org/pandas-docs/dev/development/contributing_codebase.html#writing-tests) if fixing a bug or adding a new feature
- [ ] All [code checks passed](https://pandas.pydata.org/pandas-docs/dev/development/contributing_codebase.html#pre-commit).
- [ ] Added [type annotations](https://pandas.pydata.org/pandas-docs/dev/development/contributing_codebase.html#type-hints) to new arguments/methods/functions.
- [ ] Added an entry in the latest `doc/source/whatsnew/vX.X.X.rst` file if fixing a bug or adding a new feature.
| https://api.github.com/repos/pandas-dev/pandas/pulls/57078 | 2024-01-26T01:25:06Z | 2024-01-26T19:59:36Z | 2024-01-26T19:59:36Z | 2024-01-27T18:03:20Z |
BUG: PeriodIndex.asfreq silently converts offset to period | diff --git a/pandas/core/indexes/period.py b/pandas/core/indexes/period.py
index b2f1933800fd3..563c4308d8d43 100644
--- a/pandas/core/indexes/period.py
+++ b/pandas/core/indexes/period.py
@@ -17,7 +17,10 @@
Resolution,
Tick,
)
-from pandas._libs.tslibs.dtypes import OFFSET_TO_PERIOD_FREQSTR
+from pandas._libs.tslibs.dtypes import (
+ OFFSET_TO_PERIOD_FREQSTR,
+ freq_to_period_freqstr,
+)
from pandas.util._decorators import (
cache_readonly,
doc,
@@ -205,6 +208,15 @@ def _resolution_obj(self) -> Resolution:
**_shared_doc_kwargs,
)
def asfreq(self, freq=None, how: str = "E") -> Self:
+ if isinstance(freq, BaseOffset):
+ if hasattr(freq, "_period_dtype_code"):
+ freq = freq_to_period_freqstr(freq.n, freq.name)
+ else:
+ raise ValueError(
+ f"Invalid offset: '{freq.base}' for converting time series "
+ f"with PeriodIndex."
+ )
+
arr = self._data.asfreq(freq, how)
return type(self)._simple_new(arr, name=self.name)
diff --git a/pandas/tests/indexes/period/methods/test_asfreq.py b/pandas/tests/indexes/period/methods/test_asfreq.py
index ed078a3e8fb8b..2d5214e8578ee 100644
--- a/pandas/tests/indexes/period/methods/test_asfreq.py
+++ b/pandas/tests/indexes/period/methods/test_asfreq.py
@@ -7,6 +7,8 @@
)
import pandas._testing as tm
+from pandas.tseries import offsets
+
class TestPeriodIndex:
def test_asfreq(self):
@@ -136,3 +138,18 @@ def test_asfreq_with_different_n(self):
excepted = Series([1, 2], index=PeriodIndex(["2020-02", "2020-04"], freq="M"))
tm.assert_series_equal(result, excepted)
+
+ @pytest.mark.parametrize(
+ "freq",
+ [
+ offsets.MonthBegin(2),
+ offsets.BusinessMonthEnd(2),
+ ],
+ )
+ def test_pi_asfreq_invalid_baseoffset(self, freq):
+ # GH#55785
+ msg = f"Invalid offset: '{freq.base}' for converting time series "
+
+ pi = PeriodIndex(["2020-01-01", "2021-01-01"], freq="M")
+ with pytest.raises(ValueError, match=msg):
+ pi.asfreq(freq=freq)
| xref #52064
`PeriodIndex.asfreq` silently converts for offsets such as `offsets.MonthBegin()`, `offsets.BusinessMonthEnd()`, etc. (with no attribute `'_period_dtype_code'`) frequency to period frequency (in this case `'M'`).
Reproducible Example:
```
>>> import pandas as pd
>>> from pandas.tseries import offsets
>>>
>>> index = pd.PeriodIndex(["2020-01-01", "2021-01-01"], freq="Q")
>>> index.asfreq(freq=offsets.MonthBegin(1))
PeriodIndex(['2020-03', '2021-03'], dtype='period[M]')
>>>
>>> index.asfreq(freq=offsets.BusinessMonthEnd(1))
PeriodIndex(['2020-03', '2021-03'], dtype='period[M]')
```
the correct behaviour would be raising an Error:
```
>>> index.asfreq(freq=offsets.MonthBegin(1))
ValueError: <MonthBegin> is not supported as period frequency
>>>
>>> index.asfreq(freq=offsets.BusinessMonthEnd(1))
ValueError: <BusinessMonthEnd> is not supported as period frequency
```
corrected the definition of `PeriodIndex.asfreq`and added tests. | https://api.github.com/repos/pandas-dev/pandas/pulls/57075 | 2024-01-25T22:30:17Z | 2024-02-07T14:04:08Z | null | 2024-02-07T14:04:08Z |
BUG: Index.join with different names doesn't return None name | diff --git a/doc/source/whatsnew/v3.0.0.rst b/doc/source/whatsnew/v3.0.0.rst
index 915b7744302c0..7e731462b1e67 100644
--- a/doc/source/whatsnew/v3.0.0.rst
+++ b/doc/source/whatsnew/v3.0.0.rst
@@ -157,9 +157,6 @@ Performance improvements
Bug fixes
~~~~~~~~~
-- Fixed bug in :meth:`DataFrame.join` inconsistently setting result index name (:issue:`55815`)
-- Fixed bug in :meth:`DataFrame.to_string` that raised ``StopIteration`` with nested DataFrames. (:issue:`16098`)
-- Fixed bug in :meth:`Series.diff` allowing non-integer values for the ``periods`` argument. (:issue:`56607`)
Categorical
^^^^^^^^^^^
@@ -188,9 +185,11 @@ Numeric
Conversion
^^^^^^^^^^
+- Bug in :meth:`DataFrame.to_string` that raised ``StopIteration`` with nested DataFrames. (:issue:`16098`)
- Bug in :meth:`Series.astype` might modify read-only array inplace when casting to a string dtype (:issue:`57212`)
- Bug in :meth:`Series.reindex` not maintaining ``float32`` type when a ``reindex`` introduces a missing value (:issue:`45857`)
+
Strings
^^^^^^^
- Bug in :meth:`Series.value_counts` would not respect ``sort=False`` for series having ``string`` dtype (:issue:`55224`)
@@ -243,8 +242,8 @@ Groupby/resample/rolling
Reshaping
^^^^^^^^^
--
--
+- Bug in :meth:`DataFrame.join` inconsistently setting result index name (:issue:`55815`)
+- Bug in :meth:`Index.join` where different :class:`Index` with different ``name`` s would not return an :class:`Index` with ``name=None`` (:issue:`57065`)
Sparse
^^^^^^
@@ -265,6 +264,8 @@ Other
^^^^^
- Bug in :meth:`DataFrame.sort_index` when passing ``axis="columns"`` and ``ignore_index=True`` and ``ascending=False`` not returning a :class:`RangeIndex` columns (:issue:`57293`)
- Bug in :meth:`DataFrame.where` where using a non-bool type array in the function would return a ``ValueError`` instead of a ``TypeError`` (:issue:`56330`)
+- Bug in :meth:`Series.diff` allowing non-integer values for the ``periods`` argument. (:issue:`56607`)
+
.. ***DO NOT USE THIS SECTION***
diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py
index d9f545969d9f7..07286c0a92f94 100644
--- a/pandas/core/indexes/base.py
+++ b/pandas/core/indexes/base.py
@@ -4451,6 +4451,8 @@ def _join_non_unique(
else:
join_index = self.take(left_idx)
+ join_index.name = ops.get_op_result_name(self, other)
+
if how == "outer":
mask = left_idx == -1
if mask.any():
diff --git a/pandas/tests/indexes/numeric/test_join.py b/pandas/tests/indexes/numeric/test_join.py
index 4737af987a04e..7b065d894c686 100644
--- a/pandas/tests/indexes/numeric/test_join.py
+++ b/pandas/tests/indexes/numeric/test_join.py
@@ -366,3 +366,12 @@ def test_join_outer(self, index_large):
tm.assert_index_equal(res, eres)
tm.assert_numpy_array_equal(lidx, elidx)
tm.assert_numpy_array_equal(ridx, eridx)
+
+
+def test_join_differing_names_none():
+ # GH 57065
+ idx1 = Index([1, 1, 2, 3, 4, 4], name="a")
+ idx2 = Index([2, 5, 3, 4], name="b")
+ result = idx1.join(idx2)
+ expected = Index([1, 1, 2, 3, 4, 4])
+ tm.assert_index_equal(result, expected)
| - [ ] closes #57065 (Replace xxxx with the GitHub issue number)
- [ ] [Tests added and passed](https://pandas.pydata.org/pandas-docs/dev/development/contributing_codebase.html#writing-tests) if fixing a bug or adding a new feature
- [ ] All [code checks passed](https://pandas.pydata.org/pandas-docs/dev/development/contributing_codebase.html#pre-commit).
- [ ] Added [type annotations](https://pandas.pydata.org/pandas-docs/dev/development/contributing_codebase.html#type-hints) to new arguments/methods/functions.
- [ ] Added an entry in the latest `doc/source/whatsnew/vX.X.X.rst` file if fixing a bug or adding a new feature.
| https://api.github.com/repos/pandas-dev/pandas/pulls/57074 | 2024-01-25T18:37:28Z | 2024-03-18T18:56:41Z | null | 2024-03-20T17:21:01Z |
Backport PR #57034 on branch 2.2.x (REGR: perf regression in Series.combine_first) | diff --git a/doc/source/whatsnew/v2.2.1.rst b/doc/source/whatsnew/v2.2.1.rst
index d3065cdd8b624..93965ffed23d3 100644
--- a/doc/source/whatsnew/v2.2.1.rst
+++ b/doc/source/whatsnew/v2.2.1.rst
@@ -13,6 +13,7 @@ including other versions of pandas.
Fixed regressions
~~~~~~~~~~~~~~~~~
+- Fixed performance regression in :meth:`Series.combine_first` (:issue:`55845`)
- Fixed regression in :func:`merge_ordered` raising ``TypeError`` for ``fill_method="ffill"`` and ``how="left"`` (:issue:`57010`)
- Fixed regression in :meth:`Series.pct_change` raising a ``ValueError`` for an empty :class:`Series` (:issue:`57056`)
diff --git a/pandas/core/series.py b/pandas/core/series.py
index f06a1eb533ba4..c6a905cbb6ec1 100644
--- a/pandas/core/series.py
+++ b/pandas/core/series.py
@@ -86,6 +86,7 @@
from pandas.core.dtypes.dtypes import (
CategoricalDtype,
ExtensionDtype,
+ SparseDtype,
)
from pandas.core.dtypes.generic import (
ABCDataFrame,
@@ -3510,6 +3511,13 @@ def combine_first(self, other) -> Series:
"""
from pandas.core.reshape.concat import concat
+ if self.dtype == other.dtype:
+ if self.index.equals(other.index):
+ return self.mask(self.isna(), other)
+ elif self._can_hold_na and not isinstance(self.dtype, SparseDtype):
+ this, other = self.align(other, join="outer")
+ return this.mask(this.isna(), other)
+
new_index = self.index.union(other.index)
this = self
| Backport PR #57034: REGR: perf regression in Series.combine_first | https://api.github.com/repos/pandas-dev/pandas/pulls/57072 | 2024-01-25T16:25:12Z | 2024-01-25T18:03:15Z | 2024-01-25T18:03:15Z | 2024-01-25T18:03:15Z |
BUG: Fix reading of multi_index dataframe with NaN | diff --git a/pandas/_libs/parsers.pyx b/pandas/_libs/parsers.pyx
index 82e9812094af2..afce66391de80 100644
--- a/pandas/_libs/parsers.pyx
+++ b/pandas/_libs/parsers.pyx
@@ -746,7 +746,8 @@ cdef class TextReader:
is not None else 0)
# if wrong number of blanks or no index, not our format
- if (lc != unnamed_count and lc - ic > unnamed_count) or ic == 0:
+ if ((lc != unnamed_count and lc - ic >= unnamed_count) or
+ ic == 0):
hr -= 1
self.parser_start -= 1
this_header = [None] * lc
diff --git a/pandas/tests/io/formats/test_to_csv.py b/pandas/tests/io/formats/test_to_csv.py
index 0db49a73621ea..0a16f2d643695 100644
--- a/pandas/tests/io/formats/test_to_csv.py
+++ b/pandas/tests/io/formats/test_to_csv.py
@@ -332,6 +332,24 @@ def test_to_csv_float_ea_no_float_format(self):
)
assert result == expected
+ def test_to_csv_multi_index_nan(self):
+ # Create a MultiIndex DataFrame
+ columns = pd.MultiIndex.from_tuples(
+ [("Level 1", "Level 2")], names=["level1", "level2"]
+ )
+ data = [[np.nan], [0.1], [0.4]]
+ df_complex = DataFrame(data, columns=columns)
+
+ # Expected DataFrame
+ expected_df = DataFrame(data, columns=columns, index=range(3))
+
+ # Save and load the DataFrame as a CSV
+ with tm.ensure_clean("complex_data.csv") as path:
+ df_complex.to_csv(path)
+ loaded_df_complex = pd.read_csv(path, header=[0, 1], index_col=0)
+
+ tm.assert_frame_equal(loaded_df_complex, expected_df)
+
def test_to_csv_multi_index(self):
# see gh-6618
df = DataFrame([1], columns=pd.MultiIndex.from_arrays([[1], [2]]))
| - [x] closes #56929
- [x] [Tests added and passed](https://pandas.pydata.org/pandas-docs/dev/development/contributing_codebase.html#writing-tests) if fixing a bug or adding a new feature
- [x] All [code checks passed](https://pandas.pydata.org/pandas-docs/dev/development/contributing_codebase.html#pre-commit).
- [ ] Added [type annotations](https://pandas.pydata.org/pandas-docs/dev/development/contributing_codebase.html#type-hints) to new arguments/methods/functions.
- [ ] Added an entry in the latest `doc/source/whatsnew/vX.X.X.rst` file if fixing a bug or adding a new feature.
See #56929 for some of the discussion around this bug. Instead of removing the additional header row as suggested in the issue, I fixed the bug _after_ reading all three rows as a header. The code that errors will have these headers: _header_: ['0', 'Unnamed: 1_level_2'], the code where we explicitly set empty values to NaN before writing has these: _header_: ['0', 'NaN']. The problem with the empty value stems from this line: https://github.com/pandas-dev/pandas/blob/736868671044605b0f2c2cc23ab5ea09fbc9a2ac/pandas/_libs/parsers.pyx#L694-L698 which checks for an empty string and then assigns Unnamed level to it (this is where the paths for the csv with explicit NaN values diverges as its not assigned the unnamed level)
I think there are several ways of fixing this. In this PR I tried to make sure that the first header is treated like the second by simply changing the comparison in this line: https://github.com/pandas-dev/pandas/blob/736868671044605b0f2c2cc23ab5ea09fbc9a2ac/pandas/_libs/parsers.pyx#L749
No other tests are failing after recompiling the code, therefore I am fairly confident that this fixes the issue and does not introduce others. However, it would still be good if someone else with more parsing experience looks over it.
Edit: It seems some tests are breaking due to this, will check it out now. | https://api.github.com/repos/pandas-dev/pandas/pulls/57070 | 2024-01-25T12:53:50Z | 2024-02-28T17:46:33Z | null | 2024-02-28T17:46:33Z |
Backport PR #57057 on branch 2.2.x (COMPAT: Make argsort compatable with numpy 2.0) | diff --git a/pandas/compat/numpy/function.py b/pandas/compat/numpy/function.py
index a36e25a9df410..4df30f7f4a8a7 100644
--- a/pandas/compat/numpy/function.py
+++ b/pandas/compat/numpy/function.py
@@ -138,6 +138,7 @@ def validate_argmax_with_skipna(skipna: bool | ndarray | None, args, kwargs) ->
ARGSORT_DEFAULTS["kind"] = "quicksort"
ARGSORT_DEFAULTS["order"] = None
ARGSORT_DEFAULTS["kind"] = None
+ARGSORT_DEFAULTS["stable"] = None
validate_argsort = CompatValidator(
@@ -149,6 +150,7 @@ def validate_argmax_with_skipna(skipna: bool | ndarray | None, args, kwargs) ->
ARGSORT_DEFAULTS_KIND: dict[str, int | None] = {}
ARGSORT_DEFAULTS_KIND["axis"] = -1
ARGSORT_DEFAULTS_KIND["order"] = None
+ARGSORT_DEFAULTS_KIND["stable"] = None
validate_argsort_kind = CompatValidator(
ARGSORT_DEFAULTS_KIND, fname="argsort", max_fname_arg_count=0, method="both"
)
diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py
index 88a08dd55f739..c36dcda6e2972 100644
--- a/pandas/core/indexes/base.py
+++ b/pandas/core/indexes/base.py
@@ -956,7 +956,7 @@ def __array_ufunc__(self, ufunc: np.ufunc, method: str_t, *inputs, **kwargs):
return self.__array_wrap__(result)
@final
- def __array_wrap__(self, result, context=None):
+ def __array_wrap__(self, result, context=None, return_scalar=False):
"""
Gets called after a ufunc and other functions e.g. np.split.
"""
diff --git a/pandas/core/series.py b/pandas/core/series.py
index 83eb545b9b681..f06a1eb533ba4 100644
--- a/pandas/core/series.py
+++ b/pandas/core/series.py
@@ -4066,6 +4066,7 @@ def argsort(
axis: Axis = 0,
kind: SortKind = "quicksort",
order: None = None,
+ stable: None = None,
) -> Series:
"""
Return the integer indices that would sort the Series values.
@@ -4082,6 +4083,8 @@ def argsort(
information. 'mergesort' and 'stable' are the only stable algorithms.
order : None
Has no effect but is accepted for compatibility with numpy.
+ stable : None
+ Has no effect but is accepted for compatibility with numpy.
Returns
-------
| Backport PR #57057: COMPAT: Make argsort compatable with numpy 2.0 | https://api.github.com/repos/pandas-dev/pandas/pulls/57062 | 2024-01-25T01:02:44Z | 2024-01-25T16:12:32Z | 2024-01-25T16:12:32Z | 2024-01-25T16:12:32Z |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.