Skip to content

Commit 5eaac26

Browse files
jbrockmendelSeeminSyed
authored andcommitted
CLN: assorted cleanups, annotations (pandas-dev#32475)
1 parent 0749715 commit 5eaac26

File tree

11 files changed

+23
-24
lines changed

11 files changed

+23
-24
lines changed

pandas/_libs/tslibs/timedeltas.pyx

+1-1
Original file line numberDiff line numberDiff line change
@@ -1167,7 +1167,7 @@ class Timedelta(_Timedelta):
11671167
11681168
Possible values:
11691169
1170-
* 'Y', 'M', 'W', 'D', 'T', 'S', 'L', 'U', or 'N'
1170+
* 'W', 'D', 'T', 'S', 'L', 'U', or 'N'
11711171
* 'days' or 'day'
11721172
* 'hours', 'hour', 'hr', or 'h'
11731173
* 'minutes', 'minute', 'min', or 'm'

pandas/core/arrays/datetimes.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -988,7 +988,7 @@ def tz_localize(self, tz, ambiguous="raise", nonexistent="raise"):
988988
# ----------------------------------------------------------------
989989
# Conversion Methods - Vectorized analogues of Timestamp methods
990990

991-
def to_pydatetime(self):
991+
def to_pydatetime(self) -> np.ndarray:
992992
"""
993993
Return Datetime Array/Index as object ndarray of datetime.datetime
994994
objects.

pandas/core/arrays/timedeltas.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -825,7 +825,7 @@ def total_seconds(self):
825825
"""
826826
return self._maybe_mask_results(1e-9 * self.asi8, fill_value=None)
827827

828-
def to_pytimedelta(self):
828+
def to_pytimedelta(self) -> np.ndarray:
829829
"""
830830
Return Timedelta Array/Index as object ndarray of datetime.timedelta
831831
objects.

pandas/core/indexes/accessors.py

+10-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
"""
22
datetimelike delegation
33
"""
4+
from typing import TYPE_CHECKING
5+
46
import numpy as np
57

68
from pandas.core.dtypes.common import (
@@ -21,9 +23,12 @@
2123
from pandas.core.indexes.datetimes import DatetimeIndex
2224
from pandas.core.indexes.timedeltas import TimedeltaIndex
2325

26+
if TYPE_CHECKING:
27+
from pandas import Series # noqa:F401
28+
2429

2530
class Properties(PandasDelegate, PandasObject, NoNewAttributesMixin):
26-
def __init__(self, data, orig):
31+
def __init__(self, data: "Series", orig):
2732
if not isinstance(data, ABCSeries):
2833
raise TypeError(
2934
f"cannot convert an object of type {type(data)} to a datetimelike index"
@@ -137,7 +142,7 @@ class DatetimeProperties(Properties):
137142
Raises TypeError if the Series does not contain datetimelike values.
138143
"""
139144

140-
def to_pydatetime(self):
145+
def to_pydatetime(self) -> np.ndarray:
141146
"""
142147
Return the data as an array of native Python datetime objects.
143148
@@ -209,7 +214,7 @@ class TimedeltaProperties(Properties):
209214
Raises TypeError if the Series does not contain datetimelike values.
210215
"""
211216

212-
def to_pytimedelta(self):
217+
def to_pytimedelta(self) -> np.ndarray:
213218
"""
214219
Return an array of native `datetime.timedelta` objects.
215220
@@ -271,7 +276,7 @@ def components(self):
271276
2 0 0 0 2 0 0 0
272277
3 0 0 0 3 0 0 0
273278
4 0 0 0 4 0 0 0
274-
""" # noqa: E501
279+
"""
275280
return self._get_values().components.set_index(self._parent.index)
276281

277282
@property
@@ -303,7 +308,7 @@ class PeriodProperties(Properties):
303308
class CombinedDatetimelikeProperties(
304309
DatetimeProperties, TimedeltaProperties, PeriodProperties
305310
):
306-
def __new__(cls, data):
311+
def __new__(cls, data: "Series"):
307312
# CombinedDatetimelikeProperties isn't really instantiated. Instead
308313
# we need to choose which parent (datetime or timedelta) is
309314
# appropriate. Since we're checking the dtypes anyway, we'll just

pandas/core/indexes/range.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ def _data(self):
168168
return self._cached_data
169169

170170
@cache_readonly
171-
def _int64index(self):
171+
def _int64index(self) -> Int64Index:
172172
return Int64Index._simple_new(self._data, name=self.name)
173173

174174
def _get_data_as_items(self):

pandas/core/internals/managers.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,14 @@
2424
is_list_like,
2525
is_numeric_v_string_like,
2626
is_scalar,
27-
is_sparse,
2827
)
2928
from pandas.core.dtypes.concat import concat_compat
3029
from pandas.core.dtypes.dtypes import ExtensionDtype
3130
from pandas.core.dtypes.generic import ABCExtensionArray, ABCSeries
3231
from pandas.core.dtypes.missing import isna
3332

3433
import pandas.core.algorithms as algos
34+
from pandas.core.arrays.sparse import SparseDtype
3535
from pandas.core.base import PandasObject
3636
from pandas.core.indexers import maybe_convert_indices
3737
from pandas.core.indexes.api import Index, MultiIndex, ensure_index
@@ -843,8 +843,8 @@ def _interleave(self) -> np.ndarray:
843843

844844
# TODO: https://github.com/pandas-dev/pandas/issues/22791
845845
# Give EAs some input on what happens here. Sparse needs this.
846-
if is_sparse(dtype):
847-
dtype = dtype.subtype # type: ignore
846+
if isinstance(dtype, SparseDtype):
847+
dtype = dtype.subtype
848848
elif is_extension_array_dtype(dtype):
849849
dtype = "object"
850850

pandas/core/nanops.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -981,7 +981,7 @@ def nanskew(
981981
Examples
982982
--------
983983
>>> import pandas.core.nanops as nanops
984-
>>> s = pd.Series([1,np.nan, 1, 2])
984+
>>> s = pd.Series([1, np.nan, 1, 2])
985985
>>> nanops.nanskew(s)
986986
1.7320508075688787
987987
"""
@@ -1065,7 +1065,7 @@ def nankurt(
10651065
Examples
10661066
--------
10671067
>>> import pandas.core.nanops as nanops
1068-
>>> s = pd.Series([1,np.nan, 1, 3, 2])
1068+
>>> s = pd.Series([1, np.nan, 1, 3, 2])
10691069
>>> nanops.nankurt(s)
10701070
-1.2892561983471076
10711071
"""

pandas/core/series.py

-4
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@
4747
ABCMultiIndex,
4848
ABCPeriodIndex,
4949
ABCSeries,
50-
ABCSparseArray,
5150
)
5251
from pandas.core.dtypes.inference import is_hashable
5352
from pandas.core.dtypes.missing import (
@@ -289,9 +288,6 @@ def __init__(
289288
pass
290289
elif isinstance(data, (set, frozenset)):
291290
raise TypeError(f"'{type(data).__name__}' type is unordered")
292-
elif isinstance(data, ABCSparseArray):
293-
# handle sparse passed here (and force conversion)
294-
data = data.to_dense()
295291
else:
296292
data = com.maybe_iterable_to_list(data)
297293

pandas/tests/frame/indexing/test_datetime.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ def test_set_reset(self):
4040
# set/reset
4141
df = DataFrame({"A": [0, 1, 2]}, index=idx)
4242
result = df.reset_index()
43-
assert result["foo"].dtype, "M8[ns, US/Eastern"
43+
assert result["foo"].dtype == "datetime64[ns, US/Eastern]"
4444

4545
df = result.set_index("foo")
4646
tm.assert_index_equal(df.index, idx)

pandas/tseries/frequencies.py

+2-5
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import warnings
55

66
import numpy as np
7-
from pytz import AmbiguousTimeError
87

98
from pandas._libs.algos import unique_deltas
109
from pandas._libs.tslibs import Timedelta, Timestamp
@@ -288,10 +287,7 @@ def infer_freq(index, warn: bool = True) -> Optional[str]:
288287
index = index.values
289288

290289
if not isinstance(index, pd.DatetimeIndex):
291-
try:
292-
index = pd.DatetimeIndex(index)
293-
except AmbiguousTimeError:
294-
index = pd.DatetimeIndex(index.asi8)
290+
index = pd.DatetimeIndex(index)
295291

296292
inferer = _FrequencyInferer(index, warn=warn)
297293
return inferer.get_freq()
@@ -490,6 +486,7 @@ def _is_business_daily(self) -> bool:
490486
)
491487

492488
def _get_wom_rule(self) -> Optional[str]:
489+
# FIXME: dont leave commented-out
493490
# wdiffs = unique(np.diff(self.index.week))
494491
# We also need -47, -49, -48 to catch index spanning year boundary
495492
# if not lib.ismember(wdiffs, set([4, 5, -47, -49, -48])).all():

setup.cfg

+1
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ exclude_lines =
9898
# Don't complain if non-runnable code isn't run:
9999
if 0:
100100
if __name__ == .__main__.:
101+
if TYPE_CHECKING:
101102

102103
[coverage:html]
103104
directory = coverage_html_report

0 commit comments

Comments
 (0)