Skip to content

Commit 27ff2d2

Browse files
Merge branch 'doc/clarify-parentheses-vs-brackets-62314' of https://github.com/AnandMukherjee2004/pandas into doc/clarify-parentheses-vs-brackets-62314
2 parents 6aeeabd + 10eb9aa commit 27ff2d2

File tree

27 files changed

+258
-278
lines changed

27 files changed

+258
-278
lines changed

.github/workflows/unit-tests.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -358,8 +358,6 @@ jobs:
358358
359359
- name: Run Tests
360360
uses: ./.github/actions/run-tests
361-
# TEMP allow this to fail until we fixed all test failures (related to chained assignment warnings)
362-
continue-on-error: true
363361

364362
# NOTE: this job must be kept in sync with the Pyodide build job in wheels.yml
365363
emscripten:

doc/source/whatsnew/v3.0.0.rst

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -603,6 +603,7 @@ Other API changes
603603
an empty ``RangeIndex`` or empty ``Index`` with object dtype when determining
604604
the dtype of the resulting Index (:issue:`60797`)
605605
- :class:`IncompatibleFrequency` now subclasses ``TypeError`` instead of ``ValueError``. As a result, joins with mismatched frequencies now cast to object like other non-comparable joins, and arithmetic with indexes with mismatched frequencies align (:issue:`55782`)
606+
- :meth:`CategoricalIndex.append` no longer attempts to cast different-dtype indexes to the caller's dtype (:issue:`41626`)
606607
- :meth:`ExtensionDtype.construct_array_type` is now a regular method instead of a ``classmethod`` (:issue:`58663`)
607608
- Comparison operations between :class:`Index` and :class:`Series` now consistently return :class:`Series` regardless of which object is on the left or right (:issue:`36759`)
608609
- Numpy functions like ``np.isinf`` that return a bool dtype when called on a :class:`Index` object now return a bool-dtype :class:`Index` instead of ``np.ndarray`` (:issue:`52676`)
@@ -974,8 +975,8 @@ Indexing
974975
- Bug in reindexing of :class:`DataFrame` with :class:`PeriodDtype` columns in case of consolidated block (:issue:`60980`, :issue:`60273`)
975976
- Bug in :meth:`DataFrame.loc.__getitem__` and :meth:`DataFrame.iloc.__getitem__` with a :class:`CategoricalDtype` column with integer categories raising when trying to index a row containing a ``NaN`` entry (:issue:`58954`)
976977
- Bug in :meth:`Index.__getitem__` incorrectly raising with a 0-dim ``np.ndarray`` key (:issue:`55601`)
978+
- Bug in adding new rows with :meth:`DataFrame.loc.__setitem__` or :class:`Series.loc.__setitem__` which failed to retain dtype on the object's index in some cases (:issue:`41626`)
977979
- Bug in indexing on a :class:`DatetimeIndex` with a ``timestamp[pyarrow]`` dtype or on a :class:`TimedeltaIndex` with a ``duration[pyarrow]`` dtype (:issue:`62277`)
978-
-
979980

980981
Missing
981982
^^^^^^^
@@ -1094,7 +1095,7 @@ Reshaping
10941095
- Bug in :func:`melt` where calling with duplicate column names in ``id_vars`` raised a misleading ``AttributeError`` (:issue:`61475`)
10951096
- Bug in :meth:`DataFrame.merge` where user-provided suffixes could result in duplicate column names if the resulting names matched existing columns. Now raises a :class:`MergeError` in such cases. (:issue:`61402`)
10961097
- Bug in :meth:`DataFrame.merge` with :class:`CategoricalDtype` columns incorrectly raising ``RecursionError`` (:issue:`56376`)
1097-
-
1098+
- Bug in :meth:`DataFrame.merge` with a ``float32`` index incorrectly casting the index to ``float64`` (:issue:`41626`)
10981099

10991100
Sparse
11001101
^^^^^^

pandas/_testing/contexts.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@
1212
)
1313
import uuid
1414

15-
from pandas.compat import PYPY
15+
from pandas.compat import (
16+
PYPY,
17+
WARNING_CHECK_DISABLED,
18+
)
1619
from pandas.errors import ChainedAssignmentError
1720

1821
from pandas.io.common import get_handle
@@ -163,7 +166,7 @@ def with_csv_dialect(name: str, **kwargs) -> Generator[None]:
163166
def raises_chained_assignment_error(extra_warnings=(), extra_match=()):
164167
from pandas._testing import assert_produces_warning
165168

166-
if PYPY:
169+
if PYPY or WARNING_CHECK_DISABLED:
167170
if not extra_warnings:
168171
from contextlib import nullcontext
169172

pandas/compat/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
PY312,
2222
PY314,
2323
PYPY,
24+
WARNING_CHECK_DISABLED,
2425
WASM,
2526
)
2627
from pandas.compat.numpy import is_numpy_dev
@@ -158,6 +159,7 @@ def is_ci_environment() -> bool:
158159
"PY314",
159160
"PYARROW_MIN_VERSION",
160161
"PYPY",
162+
"WARNING_CHECK_DISABLED",
161163
"WASM",
162164
"is_numpy_dev",
163165
"pa_version_under14p0",

pandas/compat/_constants.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
WASM = (sys.platform == "emscripten") or (platform.machine() in ["wasm32", "wasm64"])
2020
ISMUSL = "musl" in (sysconfig.get_config_var("HOST_GNU_TYPE") or "")
2121
REF_COUNT = 2
22+
WARNING_CHECK_DISABLED = PY314
23+
2224

2325
__all__ = [
2426
"IS64",

pandas/core/frame.py

Lines changed: 33 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,10 @@
5050
from pandas._libs.hashtable import duplicated
5151
from pandas._libs.lib import is_range_indexer
5252
from pandas.compat import PYPY
53-
from pandas.compat._constants import REF_COUNT
53+
from pandas.compat._constants import (
54+
REF_COUNT,
55+
WARNING_CHECK_DISABLED,
56+
)
5457
from pandas.compat._optional import import_optional_dependency
5558
from pandas.compat.numpy import function as nv
5659
from pandas.errors import (
@@ -4296,8 +4299,8 @@ def __setitem__(self, key, value) -> None:
42964299
z 3 50
42974300
# Values for 'a' and 'b' are completely ignored!
42984301
"""
4299-
if not PYPY:
4300-
if sys.getrefcount(self) <= 3:
4302+
if not PYPY and not WARNING_CHECK_DISABLED:
4303+
if sys.getrefcount(self) <= REF_COUNT + 1:
43014304
warnings.warn(
43024305
_chained_assignment_msg, ChainedAssignmentError, stacklevel=2
43034306
)
@@ -9211,7 +9214,7 @@ def update(
92119214
1 2 500.0
92129215
2 3 6.0
92139216
"""
9214-
if not PYPY:
9217+
if not PYPY and not WARNING_CHECK_DISABLED:
92159218
if sys.getrefcount(self) <= REF_COUNT:
92169219
warnings.warn(
92179220
_chained_assignment_method_msg,
@@ -10893,54 +10896,41 @@ def infer(x):
1089310896

1089410897
def _append_internal(
1089510898
self,
10896-
other,
10899+
other: Series,
1089710900
ignore_index: bool = False,
10898-
verify_integrity: bool = False,
10899-
sort: bool = False,
1090010901
) -> DataFrame:
10901-
if isinstance(other, (Series, dict)):
10902-
if isinstance(other, dict):
10903-
if not ignore_index:
10904-
raise TypeError("Can only append a dict if ignore_index=True")
10905-
other = Series(other)
10906-
if other.name is None and not ignore_index:
10907-
raise TypeError(
10908-
"Can only append a Series if ignore_index=True "
10909-
"or if the Series has a name"
10910-
)
10902+
assert isinstance(other, Series), type(other)
1091110903

10912-
index = Index(
10913-
[other.name],
10914-
name=(
10915-
self.index.names
10916-
if isinstance(self.index, MultiIndex)
10917-
else self.index.name
10918-
),
10904+
if other.name is None and not ignore_index:
10905+
raise TypeError(
10906+
"Can only append a Series if ignore_index=True "
10907+
"or if the Series has a name"
1091910908
)
10920-
row_df = other.to_frame().T
10921-
# infer_objects is needed for
10922-
# test_append_empty_frame_to_series_with_dateutil_tz
10923-
other = row_df.infer_objects().rename_axis(index.names)
10924-
elif isinstance(other, list):
10925-
if not other:
10926-
pass
10927-
elif not isinstance(other[0], DataFrame):
10928-
other = DataFrame(other)
10929-
if self.index.name is not None and not ignore_index:
10930-
other.index.name = self.index.name
1093110909

10932-
from pandas.core.reshape.concat import concat
10910+
index = Index(
10911+
[other.name],
10912+
name=(
10913+
self.index.names
10914+
if isinstance(self.index, MultiIndex)
10915+
else self.index.name
10916+
),
10917+
)
1093310918

10934-
if isinstance(other, (list, tuple)):
10935-
to_concat = [self, *other]
10936-
else:
10937-
to_concat = [self, other]
10919+
row_df = other.to_frame().T
10920+
if isinstance(self.index.dtype, ExtensionDtype):
10921+
# GH#41626 retain e.g. CategoricalDtype if reached via
10922+
# df.loc[key] = item
10923+
row_df.index = self.index.array._cast_pointwise_result(row_df.index._values)
10924+
10925+
# infer_objects is needed for
10926+
# test_append_empty_frame_to_series_with_dateutil_tz
10927+
row_df = row_df.infer_objects().rename_axis(index.names)
10928+
10929+
from pandas.core.reshape.concat import concat
1093810930

1093910931
result = concat(
10940-
to_concat,
10932+
[self, row_df],
1094110933
ignore_index=ignore_index,
10942-
verify_integrity=verify_integrity,
10943-
sort=sort,
1094410934
)
1094510935
return result.__finalize__(self, method="append")
1094610936

pandas/core/generic.py

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,10 @@
8383
npt,
8484
)
8585
from pandas.compat import PYPY
86-
from pandas.compat._constants import REF_COUNT
86+
from pandas.compat._constants import (
87+
REF_COUNT,
88+
WARNING_CHECK_DISABLED,
89+
)
8790
from pandas.compat._optional import import_optional_dependency
8891
from pandas.compat.numpy import function as nv
8992
from pandas.errors import (
@@ -7070,7 +7073,7 @@ def fillna(
70707073
"""
70717074
inplace = validate_bool_kwarg(inplace, "inplace")
70727075
if inplace:
7073-
if not PYPY:
7076+
if not PYPY and not WARNING_CHECK_DISABLED:
70747077
if sys.getrefcount(self) <= REF_COUNT:
70757078
warnings.warn(
70767079
_chained_assignment_method_msg,
@@ -7301,7 +7304,7 @@ def ffill(
73017304
"""
73027305
inplace = validate_bool_kwarg(inplace, "inplace")
73037306
if inplace:
7304-
if not PYPY:
7307+
if not PYPY and not WARNING_CHECK_DISABLED:
73057308
if sys.getrefcount(self) <= REF_COUNT:
73067309
warnings.warn(
73077310
_chained_assignment_method_msg,
@@ -7441,7 +7444,7 @@ def bfill(
74417444
"""
74427445
inplace = validate_bool_kwarg(inplace, "inplace")
74437446
if inplace:
7444-
if not PYPY:
7447+
if not PYPY and not WARNING_CHECK_DISABLED:
74457448
if sys.getrefcount(self) <= REF_COUNT:
74467449
warnings.warn(
74477450
_chained_assignment_method_msg,
@@ -7526,7 +7529,7 @@ def replace(
75267529

75277530
inplace = validate_bool_kwarg(inplace, "inplace")
75287531
if inplace:
7529-
if not PYPY:
7532+
if not PYPY and not WARNING_CHECK_DISABLED:
75307533
if sys.getrefcount(self) <= REF_COUNT:
75317534
warnings.warn(
75327535
_chained_assignment_method_msg,
@@ -7889,7 +7892,7 @@ def interpolate(
78897892
inplace = validate_bool_kwarg(inplace, "inplace")
78907893

78917894
if inplace:
7892-
if not PYPY:
7895+
if not PYPY and not WARNING_CHECK_DISABLED:
78937896
if sys.getrefcount(self) <= REF_COUNT:
78947897
warnings.warn(
78957898
_chained_assignment_method_msg,
@@ -8473,7 +8476,7 @@ def clip(
84738476
inplace = validate_bool_kwarg(inplace, "inplace")
84748477

84758478
if inplace:
8476-
if not PYPY:
8479+
if not PYPY and not WARNING_CHECK_DISABLED:
84778480
if sys.getrefcount(self) <= REF_COUNT:
84788481
warnings.warn(
84798482
_chained_assignment_method_msg,
@@ -10083,7 +10086,7 @@ def where(
1008310086
"""
1008410087
inplace = validate_bool_kwarg(inplace, "inplace")
1008510088
if inplace:
10086-
if not PYPY:
10089+
if not PYPY and not WARNING_CHECK_DISABLED:
1008710090
if sys.getrefcount(self) <= REF_COUNT:
1008810091
warnings.warn(
1008910092
_chained_assignment_method_msg,
@@ -10147,7 +10150,7 @@ def mask(
1014710150
) -> Self | None:
1014810151
inplace = validate_bool_kwarg(inplace, "inplace")
1014910152
if inplace:
10150-
if not PYPY:
10153+
if not PYPY and not WARNING_CHECK_DISABLED:
1015110154
if sys.getrefcount(self) <= REF_COUNT:
1015210155
warnings.warn(
1015310156
_chained_assignment_method_msg,

pandas/core/indexes/category.py

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
)
1919

2020
from pandas.core.dtypes.common import is_scalar
21-
from pandas.core.dtypes.concat import concat_compat
2221
from pandas.core.dtypes.dtypes import CategoricalDtype
2322
from pandas.core.dtypes.missing import (
2423
is_valid_na_for_dtype,
@@ -519,17 +518,3 @@ def map(self, mapper, na_action: Literal["ignore"] | None = None):
519518
"""
520519
mapped = self._values.map(mapper, na_action=na_action)
521520
return Index(mapped, name=self.name)
522-
523-
def _concat(self, to_concat: list[Index], name: Hashable) -> Index:
524-
# if calling index is category, don't check dtype of others
525-
try:
526-
cat = Categorical._concat_same_type(
527-
[self._is_dtype_compat(c) for c in to_concat]
528-
)
529-
except TypeError:
530-
# not all to_concat elements are among our categories (or NA)
531-
532-
res = concat_compat([x._values for x in to_concat])
533-
return Index(res, name=name)
534-
else:
535-
return type(self)._simple_new(cat, name=name)

0 commit comments

Comments
 (0)