Skip to content

Commit ec0c06b

Browse files
authored
CLN: TODOs and FIXMEs (#45088)
1 parent 680e372 commit ec0c06b

File tree

12 files changed

+54
-70
lines changed

12 files changed

+54
-70
lines changed

pandas/_libs/tslibs/nattype.pyx

-3
Original file line numberDiff line numberDiff line change
@@ -378,9 +378,6 @@ class NaTType(_NaT):
378378
def __reduce__(self):
379379
return (__nat_unpickle, (None, ))
380380

381-
def __rdiv__(self, other):
382-
return _nat_rdivide_op(self, other)
383-
384381
def __rtruediv__(self, other):
385382
return _nat_rdivide_op(self, other)
386383

pandas/core/algorithms.py

+6-15
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,10 @@
6363
needs_i8_conversion,
6464
)
6565
from pandas.core.dtypes.concat import concat_compat
66-
from pandas.core.dtypes.dtypes import PandasDtype
66+
from pandas.core.dtypes.dtypes import (
67+
ExtensionDtype,
68+
PandasDtype,
69+
)
6770
from pandas.core.dtypes.generic import (
6871
ABCDatetimeArray,
6972
ABCExtensionArray,
@@ -492,7 +495,7 @@ def isin(comps: AnyArrayLike, values: AnyArrayLike) -> npt.NDArray[np.bool_]:
492495
elif needs_i8_conversion(values.dtype):
493496
return isin(comps, values.astype(object))
494497

495-
elif is_extension_array_dtype(values.dtype):
498+
elif isinstance(values.dtype, ExtensionDtype):
496499
return isin(np.asarray(comps), np.asarray(values))
497500

498501
# GH16012
@@ -511,19 +514,7 @@ def f(c, v):
511514
f = np.in1d
512515

513516
else:
514-
# error: List item 0 has incompatible type "Union[Any, dtype[Any],
515-
# ExtensionDtype]"; expected "Union[dtype[Any], None, type, _SupportsDType, str,
516-
# Tuple[Any, Union[int, Sequence[int]]], List[Any], _DTypeDict, Tuple[Any,
517-
# Any]]"
518-
# error: List item 1 has incompatible type "Union[Any, ExtensionDtype]";
519-
# expected "Union[dtype[Any], None, type, _SupportsDType, str, Tuple[Any,
520-
# Union[int, Sequence[int]]], List[Any], _DTypeDict, Tuple[Any, Any]]"
521-
# error: List item 1 has incompatible type "Union[dtype[Any], ExtensionDtype]";
522-
# expected "Union[dtype[Any], None, type, _SupportsDType, str, Tuple[Any,
523-
# Union[int, Sequence[int]]], List[Any], _DTypeDict, Tuple[Any, Any]]"
524-
common = np.find_common_type(
525-
[values.dtype, comps.dtype], [] # type: ignore[list-item]
526-
)
517+
common = np.find_common_type([values.dtype, comps.dtype], [])
527518
values = values.astype(common, copy=False)
528519
comps = comps.astype(common, copy=False)
529520
f = htable.ismember

pandas/core/arrays/floating.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ def coerce_to_array(
127127
return values, mask
128128

129129
values = np.array(values, copy=copy)
130-
if is_object_dtype(values):
130+
if is_object_dtype(values.dtype):
131131
inferred_type = lib.infer_dtype(values, skipna=True)
132132
if inferred_type == "empty":
133133
pass

pandas/core/arrays/integer.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ def coerce_to_array(
177177

178178
values = np.array(values, copy=copy)
179179
inferred_type = None
180-
if is_object_dtype(values) or is_string_dtype(values):
180+
if is_object_dtype(values.dtype) or is_string_dtype(values.dtype):
181181
inferred_type = lib.infer_dtype(values, skipna=True)
182182
if inferred_type == "empty":
183183
pass

pandas/core/base.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555

5656
from pandas.core import (
5757
algorithms,
58+
nanops,
5859
ops,
5960
)
6061
from pandas.core.accessor import DirNamesMixin
@@ -70,7 +71,6 @@
7071
ensure_wrapped_if_datetimelike,
7172
extract_array,
7273
)
73-
import pandas.core.nanops as nanops
7474

7575
if TYPE_CHECKING:
7676

pandas/core/dtypes/cast.py

+12-19
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,8 @@
107107
_int32_max = np.iinfo(np.int32).max
108108
_int64_max = np.iinfo(np.int64).max
109109

110+
_dtype_obj = np.dtype(object)
111+
110112
NumpyArrayT = TypeVar("NumpyArrayT", bound=np.ndarray)
111113

112114

@@ -123,7 +125,7 @@ def maybe_convert_platform(
123125
# or ExtensionArray here.
124126
arr = values
125127

126-
if arr.dtype == object:
128+
if arr.dtype == _dtype_obj:
127129
arr = cast(np.ndarray, arr)
128130
arr = lib.maybe_convert_objects(arr)
129131

@@ -159,7 +161,7 @@ def maybe_box_datetimelike(value: Scalar, dtype: Dtype | None = None) -> Scalar:
159161
-------
160162
scalar
161163
"""
162-
if dtype == object:
164+
if dtype == _dtype_obj:
163165
pass
164166
elif isinstance(value, (np.datetime64, datetime)):
165167
value = Timestamp(value)
@@ -662,9 +664,7 @@ def _ensure_dtype_type(value, dtype: np.dtype):
662664
"""
663665
# Start with exceptions in which we do _not_ cast to numpy types
664666

665-
# error: Non-overlapping equality check (left operand type: "dtype[Any]", right
666-
# operand type: "Type[object_]")
667-
if dtype == np.object_: # type: ignore[comparison-overlap]
667+
if dtype == _dtype_obj:
668668
return value
669669

670670
# Note: before we get here we have already excluded isna(value)
@@ -1111,10 +1111,7 @@ def astype_nansafe(
11111111
raise ValueError("dtype must be np.dtype or ExtensionDtype")
11121112

11131113
if arr.dtype.kind in ["m", "M"] and (
1114-
issubclass(dtype.type, str)
1115-
# error: Non-overlapping equality check (left operand type: "dtype[Any]", right
1116-
# operand type: "Type[object]")
1117-
or dtype == object # type: ignore[comparison-overlap]
1114+
issubclass(dtype.type, str) or dtype == _dtype_obj
11181115
):
11191116
from pandas.core.construction import ensure_wrapped_if_datetimelike
11201117

@@ -1124,7 +1121,7 @@ def astype_nansafe(
11241121
if issubclass(dtype.type, str):
11251122
return lib.ensure_string_array(arr, skipna=skipna, convert_na_value=False)
11261123

1127-
elif is_datetime64_dtype(arr):
1124+
elif is_datetime64_dtype(arr.dtype):
11281125
# Non-overlapping equality check (left operand type: "dtype[Any]", right
11291126
# operand type: "Type[signedinteger[Any]]")
11301127
if dtype == np.int64: # type: ignore[comparison-overlap]
@@ -1146,7 +1143,7 @@ def astype_nansafe(
11461143

11471144
raise TypeError(f"cannot astype a datetimelike from [{arr.dtype}] to [{dtype}]")
11481145

1149-
elif is_timedelta64_dtype(arr):
1146+
elif is_timedelta64_dtype(arr.dtype):
11501147
# error: Non-overlapping equality check (left operand type: "dtype[Any]", right
11511148
# operand type: "Type[signedinteger[Any]]")
11521149
if dtype == np.int64: # type: ignore[comparison-overlap]
@@ -1170,7 +1167,7 @@ def astype_nansafe(
11701167
elif np.issubdtype(arr.dtype, np.floating) and np.issubdtype(dtype, np.integer):
11711168
return astype_float_to_int_nansafe(arr, dtype, copy)
11721169

1173-
elif is_object_dtype(arr):
1170+
elif is_object_dtype(arr.dtype):
11741171

11751172
# work around NumPy brokenness, #1987
11761173
if np.issubdtype(dtype.type, np.integer):
@@ -1718,7 +1715,7 @@ def maybe_cast_to_datetime(
17181715
# and no coercion specified
17191716
value = sanitize_to_nanoseconds(value)
17201717

1721-
elif value.dtype == object:
1718+
elif value.dtype == _dtype_obj:
17221719
value = maybe_infer_to_datetimelike(value)
17231720

17241721
elif isinstance(value, list):
@@ -1862,9 +1859,7 @@ def construct_2d_arraylike_from_scalar(
18621859

18631860
if dtype.kind in ["m", "M"]:
18641861
value = maybe_unbox_datetimelike_tz_deprecation(value, dtype)
1865-
# error: Non-overlapping equality check (left operand type: "dtype[Any]", right
1866-
# operand type: "Type[object]")
1867-
elif dtype == object: # type: ignore[comparison-overlap]
1862+
elif dtype == _dtype_obj:
18681863
if isinstance(value, (np.timedelta64, np.datetime64)):
18691864
# calling np.array below would cast to pytimedelta/pydatetime
18701865
out = np.empty(shape, dtype=object)
@@ -2190,9 +2185,7 @@ def can_hold_element(arr: ArrayLike, element: Any) -> bool:
21902185
# ExtensionBlock._can_hold_element
21912186
return True
21922187

2193-
# error: Non-overlapping equality check (left operand type: "dtype[Any]", right
2194-
# operand type: "Type[object]")
2195-
if dtype == object: # type: ignore[comparison-overlap]
2188+
if dtype == _dtype_obj:
21962189
return True
21972190

21982191
tipo = maybe_infer_dtype_type(element)

pandas/core/frame.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -7997,13 +7997,13 @@ def pivot(self, index=None, columns=None, values=None) -> DataFrame:
79977997
... aggfunc={'D': np.mean,
79987998
... 'E': [min, max, np.mean]})
79997999
>>> table
8000-
D E
8001-
mean max mean min
8000+
D E
8001+
mean max mean min
80028002
A C
8003-
bar large 5.500000 9.0 7.500000 6.0
8004-
small 5.500000 9.0 8.500000 8.0
8005-
foo large 2.000000 5.0 4.500000 4.0
8006-
small 2.333333 6.0 4.333333 2.0
8003+
bar large 5.500000 9 7.500000 6
8004+
small 5.500000 9 8.500000 8
8005+
foo large 2.000000 5 4.500000 4
8006+
small 2.333333 6 4.333333 2
80078007
"""
80088008

80098009
@Substitution("")

pandas/core/indexes/base.py

+3-4
Original file line numberDiff line numberDiff line change
@@ -443,9 +443,8 @@ def __new__(
443443
return Index._simple_new(data, name=name)
444444

445445
elif is_ea_or_datetimelike_dtype(data_dtype):
446-
# Argument 1 to "_dtype_to_subclass" of "Index" has incompatible type
447-
# "Optional[Any]"; expected "Union[dtype[Any], ExtensionDtype]" [arg-type]
448-
klass = cls._dtype_to_subclass(data_dtype) # type: ignore[arg-type]
446+
data_dtype = cast(DtypeObj, data_dtype)
447+
klass = cls._dtype_to_subclass(data_dtype)
449448
if klass is not Index:
450449
result = klass(data, copy=copy, name=name, **kwargs)
451450
if dtype is not None:
@@ -6245,7 +6244,7 @@ def _maybe_cast_slice_bound(self, label, side: str_t, kind=no_default):
62456244
# wish to have special treatment for floats/ints, e.g. Float64Index and
62466245
# datetimelike Indexes
62476246
# reject them, if index does not contain label
6248-
if (is_float(label) or is_integer(label)) and label not in self._values:
6247+
if (is_float(label) or is_integer(label)) and label not in self:
62496248
raise self._invalid_indexer("slice", label)
62506249

62516250
return label

pandas/core/internals/blocks.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -762,7 +762,7 @@ def replace_list(
762762

763763
src_len = len(pairs) - 1
764764

765-
if is_string_dtype(values):
765+
if is_string_dtype(values.dtype):
766766
# Calculate the mask once, prior to the call of comp
767767
# in order to avoid repeating the same computations
768768
mask = ~isna(values)

pandas/core/reshape/pivot.py

+15-14
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,11 @@ def pivot_table(
6060
columns=None,
6161
aggfunc: AggFuncType = "mean",
6262
fill_value=None,
63-
margins=False,
64-
dropna=True,
65-
margins_name="All",
66-
observed=False,
67-
sort=True,
63+
margins: bool = False,
64+
dropna: bool = True,
65+
margins_name: str = "All",
66+
observed: bool = False,
67+
sort: bool = True,
6868
) -> DataFrame:
6969
index = _convert_by(index)
7070
columns = _convert_by(columns)
@@ -178,13 +178,12 @@ def __internal_pivot_table(
178178
and v in agged
179179
and not is_integer_dtype(agged[v])
180180
):
181-
if isinstance(agged[v], ABCDataFrame):
181+
if not isinstance(agged[v], ABCDataFrame):
182182
# exclude DataFrame case bc maybe_downcast_to_dtype expects
183183
# ArrayLike
184-
# TODO: why does test_pivot_table_doctest_case fail if
185-
# we don't do this apparently-unnecessary setitem?
186-
agged[v] = agged[v]
187-
else:
184+
# e.g. test_pivot_table_multiindex_columns_doctest_case
185+
# agged.columns is a MultiIndex and 'v' is indexing only
186+
# on its first level.
188187
agged[v] = maybe_downcast_to_dtype(agged[v], data[v].dtype)
189188

190189
table = agged
@@ -253,7 +252,7 @@ def __internal_pivot_table(
253252

254253
def _add_margins(
255254
table: DataFrame | Series,
256-
data,
255+
data: DataFrame,
257256
values,
258257
rows,
259258
cols,
@@ -331,7 +330,7 @@ def _add_margins(
331330
return result
332331

333332

334-
def _compute_grand_margin(data, values, aggfunc, margins_name: str = "All"):
333+
def _compute_grand_margin(data: DataFrame, values, aggfunc, margins_name: str = "All"):
335334

336335
if values:
337336
grand_margin = {}
@@ -522,7 +521,7 @@ def crosstab(
522521
rownames=None,
523522
colnames=None,
524523
aggfunc=None,
525-
margins=False,
524+
margins: bool = False,
526525
margins_name: str = "All",
527526
dropna: bool = True,
528527
normalize=False,
@@ -682,7 +681,9 @@ def crosstab(
682681
return table
683682

684683

685-
def _normalize(table, normalize, margins: bool, margins_name="All"):
684+
def _normalize(
685+
table: DataFrame, normalize, margins: bool, margins_name="All"
686+
) -> DataFrame:
686687

687688
if not isinstance(normalize, (bool, str)):
688689
axis_subs = {0: "index", 1: "columns"}

pandas/core/reshape/reshape.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -686,7 +686,7 @@ def _stack_multi_column_index(columns: MultiIndex) -> MultiIndex:
686686

687687

688688
def _stack_multi_columns(frame, level_num=-1, dropna=True):
689-
def _convert_level_number(level_num, columns):
689+
def _convert_level_number(level_num: int, columns):
690690
"""
691691
Logic for converting the level number to something we can safely pass
692692
to swaplevel.

pandas/tests/reshape/test_pivot.py

+7-4
Original file line numberDiff line numberDiff line change
@@ -2084,11 +2084,12 @@ def agg(arr):
20842084
with pytest.raises(KeyError, match="notpresent"):
20852085
foo.pivot_table("notpresent", "X", "Y", aggfunc=agg)
20862086

2087-
def test_pivot_table_doctest_case(self):
2088-
# TODO: better name. the relevant characteristic is that
2089-
# the call to maybe_downcast_to_dtype(agged[v], data[v].dtype) in
2087+
def test_pivot_table_multiindex_columns_doctest_case(self):
2088+
# The relevant characteristic is that the call
2089+
# to maybe_downcast_to_dtype(agged[v], data[v].dtype) in
20902090
# __internal_pivot_table has `agged[v]` a DataFrame instead of Series,
2091-
# i.e agged.columns is not unique
2091+
# In this case this is because agged.columns is a MultiIndex and 'v'
2092+
# is only indexing on its first level.
20922093
df = DataFrame(
20932094
{
20942095
"A": ["foo", "foo", "foo", "foo", "foo", "bar", "bar", "bar", "bar"],
@@ -2131,6 +2132,8 @@ def test_pivot_table_doctest_case(self):
21312132
]
21322133
)
21332134
expected = DataFrame(vals, columns=cols, index=index)
2135+
expected[("E", "min")] = expected[("E", "min")].astype(np.int64)
2136+
expected[("E", "max")] = expected[("E", "max")].astype(np.int64)
21342137
tm.assert_frame_equal(table, expected)
21352138

21362139
def test_pivot_table_sort_false(self):

0 commit comments

Comments
 (0)