Skip to content

Commit 1681cc7

Browse files
authored
CLN: assorted (#55359)
* CLN: assorted * docstring fixup * docstring fixup * revert doc
1 parent 2983464 commit 1681cc7

File tree

8 files changed

+68
-15
lines changed

8 files changed

+68
-15
lines changed

doc/redirects.csv

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,6 @@ generated/pandas.api.types.is_number,../reference/api/pandas.api.types.is_number
127127
generated/pandas.api.types.is_numeric_dtype,../reference/api/pandas.api.types.is_numeric_dtype
128128
generated/pandas.api.types.is_object_dtype,../reference/api/pandas.api.types.is_object_dtype
129129
generated/pandas.api.types.is_period_dtype,../reference/api/pandas.api.types.is_period_dtype
130-
generated/pandas.api.types.is_period,../reference/api/pandas.api.types.is_period
131130
generated/pandas.api.types.is_re_compilable,../reference/api/pandas.api.types.is_re_compilable
132131
generated/pandas.api.types.is_re,../reference/api/pandas.api.types.is_re
133132
generated/pandas.api.types.is_scalar,../reference/api/pandas.api.types.is_scalar

doc/source/development/contributing_codebase.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -528,7 +528,7 @@ If a test is known to fail but the manner in which it fails
528528
is not meant to be captured, use ``pytest.mark.xfail`` It is common to use this method for a test that
529529
exhibits buggy behavior or a non-implemented feature. If
530530
the failing test has flaky behavior, use the argument ``strict=False``. This
531-
will make it so pytest does not fail if the test happens to pass.
531+
will make it so pytest does not fail if the test happens to pass. Using ``strict=False`` is highly undesirable, please use it only as a last resort.
532532

533533
Prefer the decorator ``@pytest.mark.xfail`` and the argument ``pytest.param``
534534
over usage within a test so that the test is appropriately marked during the

pandas/core/arrays/base.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1729,6 +1729,17 @@ def transpose(self, *axes: int) -> ExtensionArray:
17291729
17301730
Because ExtensionArrays are always 1D, this is a no-op. It is included
17311731
for compatibility with np.ndarray.
1732+
1733+
Returns
1734+
-------
1735+
ExtensionArray
1736+
1737+
Examples
1738+
--------
1739+
>>> pd.array([1, 2, 3]).transpose()
1740+
<IntegerArray>
1741+
[1, 2, 3]
1742+
Length: 3, dtype: Int64
17321743
"""
17331744
return self[:]
17341745

pandas/core/indexes/base.py

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3501,7 +3501,7 @@ def _intersection(self, other: Index, sort: bool = False):
35013501
pass
35023502
else:
35033503
# TODO: algos.unique1d should preserve DTA/TDA
3504-
if is_numeric_dtype(self):
3504+
if is_numeric_dtype(self.dtype):
35053505
# This is faster, because Index.unique() checks for uniqueness
35063506
# before calculating the unique values.
35073507
res = algos.unique1d(res_indexer)
@@ -5020,7 +5020,10 @@ def _can_use_libjoin(self) -> bool:
50205020
)
50215021
# Exclude index types where the conversion to numpy converts to object dtype,
50225022
# which negates the performance benefit of libjoin
5023-
# TODO: exclude RangeIndex? Seems to break test_concat_datetime_timezone
5023+
# Subclasses should override to return False if _get_join_target is
5024+
# not zero-copy.
5025+
# TODO: exclude RangeIndex (which allocates memory)?
5026+
# Doing so seems to break test_concat_datetime_timezone
50245027
return not isinstance(self, (ABCIntervalIndex, ABCMultiIndex))
50255028

50265029
# --------------------------------------------------------------------
@@ -6176,8 +6179,8 @@ def _get_indexer_non_comparable(
61766179
If doing an inequality check, i.e. method is not None.
61776180
"""
61786181
if method is not None:
6179-
other = _unpack_nested_dtype(target)
6180-
raise TypeError(f"Cannot compare dtypes {self.dtype} and {other.dtype}")
6182+
other_dtype = _unpack_nested_dtype(target)
6183+
raise TypeError(f"Cannot compare dtypes {self.dtype} and {other_dtype}")
61816184

61826185
no_matches = -1 * np.ones(target.shape, dtype=np.intp)
61836186
if unique:
@@ -6288,8 +6291,7 @@ def _should_compare(self, other: Index) -> bool:
62886291
# respectively.
62896292
return False
62906293

6291-
other = _unpack_nested_dtype(other)
6292-
dtype = other.dtype
6294+
dtype = _unpack_nested_dtype(other)
62936295
return self._is_comparable_dtype(dtype) or is_object_dtype(dtype)
62946296

62956297
def _is_comparable_dtype(self, dtype: DtypeObj) -> bool:
@@ -7592,7 +7594,7 @@ def get_unanimous_names(*indexes: Index) -> tuple[Hashable, ...]:
75927594
return names
75937595

75947596

7595-
def _unpack_nested_dtype(other: Index) -> Index:
7597+
def _unpack_nested_dtype(other: Index) -> DtypeObj:
75967598
"""
75977599
When checking if our dtype is comparable with another, we need
75987600
to unpack CategoricalDtype to look at its categories.dtype.
@@ -7603,20 +7605,20 @@ def _unpack_nested_dtype(other: Index) -> Index:
76037605
76047606
Returns
76057607
-------
7606-
Index
7608+
np.dtype or ExtensionDtype
76077609
"""
76087610
dtype = other.dtype
76097611
if isinstance(dtype, CategoricalDtype):
76107612
# If there is ever a SparseIndex, this could get dispatched
76117613
# here too.
7612-
return dtype.categories
7614+
return dtype.categories.dtype
76137615
elif isinstance(dtype, ArrowDtype):
76147616
# GH 53617
76157617
import pyarrow as pa
76167618

76177619
if pa.types.is_dictionary(dtype.pyarrow_dtype):
7618-
other = other.astype(ArrowDtype(dtype.pyarrow_dtype.value_type))
7619-
return other
7620+
other = other[:0].astype(ArrowDtype(dtype.pyarrow_dtype.value_type))
7621+
return other.dtype
76207622

76217623

76227624
def _maybe_try_sort(result: Index | ArrayLike, sort: bool | None):

pandas/core/series.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4004,7 +4004,8 @@ def argsort(
40044004

40054005
if mask.any():
40064006
# TODO(3.0): once this deprecation is enforced we can call
4007-
# self.array.argsort directly, which will close GH#43840
4007+
# self.array.argsort directly, which will close GH#43840 and
4008+
# GH#12694
40084009
warnings.warn(
40094010
"The behavior of Series.argsort in the presence of NA values is "
40104011
"deprecated. In a future version, NA values will be ordered "

pandas/core/sorting.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ def get_indexer_indexer(
8888
# error: Incompatible types in assignment (expression has type
8989
# "Union[ExtensionArray, ndarray[Any, Any], Index, Series]", variable has
9090
# type "Index")
91-
target = ensure_key_mapped(target, key, levels=level) # type:ignore[assignment]
91+
target = ensure_key_mapped(target, key, levels=level) # type: ignore[assignment]
9292
target = target._sort_levels_monotonic()
9393

9494
if level is not None:

pandas/tests/indexing/test_loc.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2166,6 +2166,19 @@ def test_loc_setitem_with_expansion_preserves_nullable_int(self, dtype):
21662166
result.loc[df.index, "data"] = ser._values
21672167
tm.assert_frame_equal(result, df)
21682168

2169+
def test_loc_setitem_ea_not_full_column(self):
2170+
# GH#39163
2171+
df = DataFrame({"A": range(5)})
2172+
2173+
val = date_range("2016-01-01", periods=3, tz="US/Pacific")
2174+
2175+
df.loc[[0, 1, 2], "B"] = val
2176+
2177+
bex = val.append(DatetimeIndex([pd.NaT, pd.NaT], dtype=val.dtype))
2178+
expected = DataFrame({"A": range(5), "B": bex})
2179+
assert expected.dtypes["B"] == val.dtype
2180+
tm.assert_frame_equal(df, expected)
2181+
21692182

21702183
class TestLocCallable:
21712184
def test_frame_loc_getitem_callable(self):

pandas/tests/tslibs/test_npy_units.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import numpy as np
2+
3+
from pandas._libs.tslibs.dtypes import abbrev_to_npy_unit
4+
from pandas._libs.tslibs.vectorized import is_date_array_normalized
5+
6+
# a datetime64 ndarray which *is* normalized
7+
day_arr = np.arange(10, dtype="i8").view("M8[D]")
8+
9+
10+
class TestIsDateArrayNormalized:
11+
def test_is_date_array_normalized_day(self):
12+
arr = day_arr
13+
abbrev = "D"
14+
unit = abbrev_to_npy_unit(abbrev)
15+
result = is_date_array_normalized(arr.view("i8"), None, unit)
16+
assert result is True
17+
18+
def test_is_date_array_normalized_seconds(self):
19+
abbrev = "s"
20+
arr = day_arr.astype(f"M8[{abbrev}]")
21+
unit = abbrev_to_npy_unit(abbrev)
22+
result = is_date_array_normalized(arr.view("i8"), None, unit)
23+
assert result is True
24+
25+
arr[0] += np.timedelta64(1, abbrev)
26+
result2 = is_date_array_normalized(arr.view("i8"), None, unit)
27+
assert result2 is False

0 commit comments

Comments
 (0)