Skip to content

Commit 630832b

Browse files
LarWongMarcoGorelli
authored andcommitted
TYP overload fillna pandas-dev#40737 (pandas-dev#40887)
* TYP: Added overloads for fillna() in frame.py and series.py * TYP: Added overloads for fillna() in frame.py and series.py pandas-dev#40737 * TYP: Added fillna() overloads to generic.py pandas-dev#40727 * TYP: removed generic overloads pandas-dev#40737 * fixed redundant cast error * reverting prior changes * remove cast again * removed unnecessary overloads in frame.py and series.py * fixed overloads * reverted value typing * remove extra types (lets keep this to overloads) Co-authored-by: Marco Gorelli <[email protected]>
1 parent 07d8ee3 commit 630832b

File tree

1 file changed

+43
-63
lines changed

1 file changed

+43
-63
lines changed

pandas/core/series.py

+43-63
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@
6060
from pandas.core.dtypes.cast import (
6161
convert_dtypes,
6262
maybe_box_native,
63-
maybe_cast_pointwise_result,
63+
maybe_cast_result,
6464
validate_numeric_casting,
6565
)
6666
from pandas.core.dtypes.common import (
@@ -100,7 +100,6 @@
100100
import pandas.core.common as com
101101
from pandas.core.construction import (
102102
create_series_with_explicit_dtype,
103-
ensure_wrapped_if_datetimelike,
104103
extract_array,
105104
is_empty_data,
106105
sanitize_array,
@@ -113,15 +112,15 @@
113112
from pandas.core.indexes.accessors import CombinedDatetimelikeProperties
114113
from pandas.core.indexes.api import (
115114
CategoricalIndex,
116-
DatetimeIndex,
117115
Float64Index,
118116
Index,
119117
MultiIndex,
120-
PeriodIndex,
121-
TimedeltaIndex,
122118
ensure_index,
123119
)
124120
import pandas.core.indexes.base as ibase
121+
from pandas.core.indexes.datetimes import DatetimeIndex
122+
from pandas.core.indexes.period import PeriodIndex
123+
from pandas.core.indexes.timedeltas import TimedeltaIndex
125124
from pandas.core.indexing import check_bool_indexer
126125
from pandas.core.internals import (
127126
SingleArrayManager,
@@ -864,7 +863,7 @@ def take(self, indices, axis=0, is_copy=None, **kwargs) -> Series:
864863
result = self._constructor(new_values, index=new_index, fastpath=True)
865864
return result.__finalize__(self, method="take")
866865

867-
def _take_with_is_copy(self, indices, axis=0) -> Series:
866+
def _take_with_is_copy(self, indices, axis=0):
868867
"""
869868
Internal version of the `take` method that sets the `_is_copy`
870869
attribute to keep track of the parent dataframe (using in indexing
@@ -1020,7 +1019,7 @@ def _get_value(self, label, takeable: bool = False):
10201019
loc = self.index.get_loc(label)
10211020
return self.index._get_values_for_loc(self, loc, label)
10221021

1023-
def __setitem__(self, key, value) -> None:
1022+
def __setitem__(self, key, value):
10241023
key = com.apply_if_callable(key, self)
10251024
cacher_needs_updating = self._check_is_chained_assignment_possible()
10261025

@@ -1059,7 +1058,7 @@ def __setitem__(self, key, value) -> None:
10591058
if cacher_needs_updating:
10601059
self._maybe_update_cacher()
10611060

1062-
def _set_with_engine(self, key, value) -> None:
1061+
def _set_with_engine(self, key, value):
10631062
# fails with AttributeError for IntervalIndex
10641063
loc = self.index._engine.get_loc(key)
10651064
# error: Argument 1 to "validate_numeric_casting" has incompatible type
@@ -1095,15 +1094,15 @@ def _set_with(self, key, value):
10951094
else:
10961095
self.loc[key] = value
10971096

1098-
def _set_labels(self, key, value) -> None:
1097+
def _set_labels(self, key, value):
10991098
key = com.asarray_tuplesafe(key)
11001099
indexer: np.ndarray = self.index.get_indexer(key)
11011100
mask = indexer == -1
11021101
if mask.any():
11031102
raise KeyError(f"{key[mask]} not in index")
11041103
self._set_values(indexer, value)
11051104

1106-
def _set_values(self, key, value) -> None:
1105+
def _set_values(self, key, value):
11071106
if isinstance(key, Series):
11081107
key = key._values
11091108

@@ -1892,7 +1891,7 @@ def count(self, level=None):
18921891
2
18931892
"""
18941893
if level is None:
1895-
return notna(self._values).sum().astype("int64")
1894+
return notna(self._values).sum()
18961895
else:
18971896
warnings.warn(
18981897
"Using the level keyword in DataFrame and Series aggregations is "
@@ -1994,12 +1993,15 @@ def unique(self) -> ArrayLike:
19941993
['2016-01-01 00:00:00-05:00']
19951994
Length: 1, dtype: datetime64[ns, US/Eastern]
19961995
1997-
An Categorical will return categories in the order of
1998-
appearance and with the same dtype.
1996+
An unordered Categorical will return categories in the order of
1997+
appearance.
19991998
20001999
>>> pd.Series(pd.Categorical(list('baabc'))).unique()
20012000
['b', 'a', 'c']
2002-
Categories (3, object): ['a', 'b', 'c']
2001+
Categories (3, object): ['b', 'a', 'c']
2002+
2003+
An ordered Categorical preserves the category ordering.
2004+
20032005
>>> pd.Series(pd.Categorical(list('baabc'), categories=list('abc'),
20042006
... ordered=True)).unique()
20052007
['b', 'a', 'c']
@@ -2754,15 +2756,13 @@ def __rmatmul__(self, other):
27542756
return self.dot(np.transpose(other))
27552757

27562758
@doc(base.IndexOpsMixin.searchsorted, klass="Series")
2757-
def searchsorted(self, value, side="left", sorter=None) -> np.ndarray:
2759+
def searchsorted(self, value, side="left", sorter=None):
27582760
return algorithms.searchsorted(self._values, value, side=side, sorter=sorter)
27592761

27602762
# -------------------------------------------------------------------
27612763
# Combination
27622764

2763-
def append(
2764-
self, to_append, ignore_index: bool = False, verify_integrity: bool = False
2765-
):
2765+
def append(self, to_append, ignore_index=False, verify_integrity=False):
27662766
"""
27672767
Concatenate two or more Series.
27682768
@@ -2846,7 +2846,7 @@ def append(
28462846
to_concat, ignore_index=ignore_index, verify_integrity=verify_integrity
28472847
)
28482848

2849-
def _binop(self, other: Series, func, level=None, fill_value=None):
2849+
def _binop(self, other, func, level=None, fill_value=None):
28502850
"""
28512851
Perform generic binary operation with optional fill value.
28522852
@@ -2873,7 +2873,7 @@ def _binop(self, other: Series, func, level=None, fill_value=None):
28732873
if not self.index.equals(other.index):
28742874
this, other = self.align(other, level=level, join="outer", copy=False)
28752875

2876-
this_vals, other_vals = ops.fill_binop(this._values, other._values, fill_value)
2876+
this_vals, other_vals = ops.fill_binop(this.values, other.values, fill_value)
28772877

28782878
with np.errstate(all="ignore"):
28792879
result = func(this_vals, other_vals)
@@ -3071,24 +3071,22 @@ def combine(self, other, func, fill_value=None) -> Series:
30713071
# so do this element by element
30723072
new_index = self.index.union(other.index)
30733073
new_name = ops.get_op_result_name(self, other)
3074-
new_values = np.empty(len(new_index), dtype=object)
3075-
for i, idx in enumerate(new_index):
3074+
new_values = []
3075+
for idx in new_index:
30763076
lv = self.get(idx, fill_value)
30773077
rv = other.get(idx, fill_value)
30783078
with np.errstate(all="ignore"):
3079-
new_values[i] = func(lv, rv)
3079+
new_values.append(func(lv, rv))
30803080
else:
30813081
# Assume that other is a scalar, so apply the function for
30823082
# each element in the Series
30833083
new_index = self.index
3084-
new_values = np.empty(len(new_index), dtype=object)
30853084
with np.errstate(all="ignore"):
3086-
new_values[:] = [func(lv, other) for lv in self._values]
3085+
new_values = [func(lv, other) for lv in self._values]
30873086
new_name = self.name
30883087

3089-
# try_float=False is to match _aggregate_series_pure_python
3090-
npvalues = lib.maybe_convert_objects(new_values, try_float=False)
3091-
res_values = maybe_cast_pointwise_result(npvalues, self.dtype, same_dtype=False)
3088+
res_values = sanitize_array(new_values, None)
3089+
res_values = maybe_cast_result(res_values, self.dtype, same_dtype=False)
30923090
return self._constructor(res_values, index=new_index, name=new_name)
30933091

30943092
def combine_first(self, other) -> Series:
@@ -3611,7 +3609,7 @@ def argsort(self, axis=0, kind="quicksort", order=None) -> Series:
36113609
36123610
Returns
36133611
-------
3614-
Series[np.intp]
3612+
Series
36153613
Positions of values within the sort order with -1 indicating
36163614
nan values.
36173615
@@ -3732,7 +3730,7 @@ def nlargest(self, n=5, keep="first") -> Series:
37323730
"""
37333731
return algorithms.SelectNSeries(self, n=n, keep=keep).nlargest()
37343732

3735-
def nsmallest(self, n: int = 5, keep: str = "first") -> Series:
3733+
def nsmallest(self, n=5, keep="first") -> Series:
37363734
"""
37373735
Return the smallest `n` elements.
37383736
@@ -3944,7 +3942,7 @@ def explode(self, ignore_index: bool = False) -> Series:
39443942

39453943
return self._constructor(values, index=index, name=self.name)
39463944

3947-
def unstack(self, level=-1, fill_value=None) -> DataFrame:
3945+
def unstack(self, level=-1, fill_value=None):
39483946
"""
39493947
Unstack, also known as pivot, Series with MultiIndex to produce DataFrame.
39503948
@@ -4169,8 +4167,7 @@ def apply(
41694167
Python function or NumPy ufunc to apply.
41704168
convert_dtype : bool, default True
41714169
Try to find better dtype for elementwise function results. If
4172-
False, leave as dtype=object. Note that the dtype is always
4173-
preserved for extension array dtypes, such as Categorical.
4170+
False, leave as dtype=object.
41744171
args : tuple
41754172
Positional arguments passed to func after the series value.
41764173
**kwargs
@@ -4190,7 +4187,7 @@ def apply(
41904187
Notes
41914188
-----
41924189
Functions that mutate the passed object can produce unexpected
4193-
behavior or errors and are not supported. See :ref:`gotchas.udf-mutation`
4190+
behavior or errors and are not supported. See :ref:`udf-mutation`
41944191
for more details.
41954192
41964193
Examples
@@ -4297,11 +4294,7 @@ def _reduce(
42974294
with np.errstate(all="ignore"):
42984295
return op(delegate, skipna=skipna, **kwds)
42994296

4300-
def _reindex_indexer(
4301-
self, new_index: Index | None, indexer: np.ndarray | None, copy: bool
4302-
) -> Series:
4303-
# Note: new_index is None iff indexer is None
4304-
# if not None, indexer is np.intp
4297+
def _reindex_indexer(self, new_index, indexer, copy):
43054298
if indexer is None:
43064299
if copy:
43074300
return self.copy()
@@ -4319,9 +4312,8 @@ def _needs_reindex_multi(self, axes, method, level) -> bool:
43194312
"""
43204313
return False
43214314

4322-
# error: Cannot determine type of 'align'
43234315
@doc(
4324-
NDFrame.align, # type: ignore[has-type]
4316+
NDFrame.align,
43254317
klass=_shared_doc_kwargs["klass"],
43264318
axes_single_arg=_shared_doc_kwargs["axes_single_arg"],
43274319
)
@@ -4473,9 +4465,8 @@ def set_axis(self, labels, axis: Axis = ..., inplace: bool = ...) -> Series | No
44734465
def set_axis(self, labels, axis: Axis = 0, inplace: bool = False):
44744466
return super().set_axis(labels, axis=axis, inplace=inplace)
44754467

4476-
# error: Cannot determine type of 'reindex'
44774468
@doc(
4478-
NDFrame.reindex, # type: ignore[has-type]
4469+
NDFrame.reindex,
44794470
klass=_shared_doc_kwargs["klass"],
44804471
axes=_shared_doc_kwargs["axes"],
44814472
optional_labels=_shared_doc_kwargs["optional_labels"],
@@ -4705,8 +4696,7 @@ def fillna(
47054696
) -> Series | None:
47064697
...
47074698

4708-
# error: Cannot determine type of 'fillna'
4709-
@doc(NDFrame.fillna, **_shared_doc_kwargs) # type: ignore[has-type]
4699+
@doc(NDFrame.fillna, **_shared_doc_kwargs)
47104700
def fillna(
47114701
self,
47124702
value=None,
@@ -4752,9 +4742,8 @@ def pop(self, item: Hashable) -> Any:
47524742
"""
47534743
return super().pop(item=item)
47544744

4755-
# error: Cannot determine type of 'replace'
47564745
@doc(
4757-
NDFrame.replace, # type: ignore[has-type]
4746+
NDFrame.replace,
47584747
klass=_shared_doc_kwargs["klass"],
47594748
inplace=_shared_doc_kwargs["inplace"],
47604749
replace_iloc=_shared_doc_kwargs["replace_iloc"],
@@ -4802,8 +4791,7 @@ def _replace_single(self, to_replace, method: str, inplace: bool, limit):
48024791

48034792
return result
48044793

4805-
# error: Cannot determine type of 'shift'
4806-
@doc(NDFrame.shift, klass=_shared_doc_kwargs["klass"]) # type: ignore[has-type]
4794+
@doc(NDFrame.shift, klass=_shared_doc_kwargs["klass"])
48074795
def shift(self, periods=1, freq=None, axis=0, fill_value=None) -> Series:
48084796
return super().shift(
48094797
periods=periods, freq=freq, axis=axis, fill_value=fill_value
@@ -5038,23 +5026,19 @@ def _convert_dtypes(
50385026
result = input_series.copy()
50395027
return result
50405028

5041-
# error: Cannot determine type of 'isna'
5042-
@doc(NDFrame.isna, klass=_shared_doc_kwargs["klass"]) # type: ignore[has-type]
5029+
@doc(NDFrame.isna, klass=_shared_doc_kwargs["klass"])
50435030
def isna(self) -> Series:
50445031
return generic.NDFrame.isna(self)
50455032

5046-
# error: Cannot determine type of 'isna'
5047-
@doc(NDFrame.isna, klass=_shared_doc_kwargs["klass"]) # type: ignore[has-type]
5033+
@doc(NDFrame.isna, klass=_shared_doc_kwargs["klass"])
50485034
def isnull(self) -> Series:
50495035
return super().isnull()
50505036

5051-
# error: Cannot determine type of 'notna'
5052-
@doc(NDFrame.notna, klass=_shared_doc_kwargs["klass"]) # type: ignore[has-type]
5037+
@doc(NDFrame.notna, klass=_shared_doc_kwargs["klass"])
50535038
def notna(self) -> Series:
50545039
return super().notna()
50555040

5056-
# error: Cannot determine type of 'notna'
5057-
@doc(NDFrame.notna, klass=_shared_doc_kwargs["klass"]) # type: ignore[has-type]
5041+
@doc(NDFrame.notna, klass=_shared_doc_kwargs["klass"])
50585042
def notnull(self) -> Series:
50595043
return super().notnull()
50605044

@@ -5149,8 +5133,7 @@ def dropna(self, axis=0, inplace=False, how=None):
51495133
# ----------------------------------------------------------------------
51505134
# Time series-oriented methods
51515135

5152-
# error: Cannot determine type of 'asfreq'
5153-
@doc(NDFrame.asfreq, **_shared_doc_kwargs) # type: ignore[has-type]
5136+
@doc(NDFrame.asfreq, **_shared_doc_kwargs)
51545137
def asfreq(
51555138
self,
51565139
freq,
@@ -5167,8 +5150,7 @@ def asfreq(
51675150
fill_value=fill_value,
51685151
)
51695152

5170-
# error: Cannot determine type of 'resample'
5171-
@doc(NDFrame.resample, **_shared_doc_kwargs) # type: ignore[has-type]
5153+
@doc(NDFrame.resample, **_shared_doc_kwargs)
51725154
def resample(
51735155
self,
51745156
rule,
@@ -5313,8 +5295,6 @@ def _arith_method(self, other, op):
53135295

53145296
lvalues = self._values
53155297
rvalues = extract_array(other, extract_numpy=True, extract_range=True)
5316-
rvalues = ops.maybe_prepare_scalar_for_op(rvalues, lvalues.shape)
5317-
rvalues = ensure_wrapped_if_datetimelike(rvalues)
53185298

53195299
with np.errstate(all="ignore"):
53205300
result = ops.arithmetic_op(lvalues, rvalues, op)

0 commit comments

Comments
 (0)