|
50 | 50 | from pandas._libs.hashtable import duplicated |
51 | 51 | from pandas._libs.lib import is_range_indexer |
52 | 52 | 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 | +) |
54 | 57 | from pandas.compat._optional import import_optional_dependency |
55 | 58 | from pandas.compat.numpy import function as nv |
56 | 59 | from pandas.errors import ( |
@@ -4296,8 +4299,8 @@ def __setitem__(self, key, value) -> None: |
4296 | 4299 | z 3 50 |
4297 | 4300 | # Values for 'a' and 'b' are completely ignored! |
4298 | 4301 | """ |
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: |
4301 | 4304 | warnings.warn( |
4302 | 4305 | _chained_assignment_msg, ChainedAssignmentError, stacklevel=2 |
4303 | 4306 | ) |
@@ -9211,7 +9214,7 @@ def update( |
9211 | 9214 | 1 2 500.0 |
9212 | 9215 | 2 3 6.0 |
9213 | 9216 | """ |
9214 | | - if not PYPY: |
| 9217 | + if not PYPY and not WARNING_CHECK_DISABLED: |
9215 | 9218 | if sys.getrefcount(self) <= REF_COUNT: |
9216 | 9219 | warnings.warn( |
9217 | 9220 | _chained_assignment_method_msg, |
@@ -10893,54 +10896,41 @@ def infer(x): |
10893 | 10896 |
|
10894 | 10897 | def _append_internal( |
10895 | 10898 | self, |
10896 | | - other, |
| 10899 | + other: Series, |
10897 | 10900 | ignore_index: bool = False, |
10898 | | - verify_integrity: bool = False, |
10899 | | - sort: bool = False, |
10900 | 10901 | ) -> 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) |
10911 | 10903 |
|
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" |
10919 | 10908 | ) |
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 |
10931 | 10909 |
|
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 | + ) |
10933 | 10918 |
|
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 |
10938 | 10930 |
|
10939 | 10931 | result = concat( |
10940 | | - to_concat, |
| 10932 | + [self, row_df], |
10941 | 10933 | ignore_index=ignore_index, |
10942 | | - verify_integrity=verify_integrity, |
10943 | | - sort=sort, |
10944 | 10934 | ) |
10945 | 10935 | return result.__finalize__(self, method="append") |
10946 | 10936 |
|
|
0 commit comments