Skip to content

Commit c38dab8

Browse files
jbrockmendelCloseChoice
authored andcommitted
CLN: assorted cleanups, annotations, de-privatizing (pandas-dev#33497)
* de-privatize * CLN: assorted cleanups, annotations
1 parent 2989c7d commit c38dab8

File tree

12 files changed

+27
-37
lines changed

12 files changed

+27
-37
lines changed

pandas/_libs/tslibs/offsets.pyx

+2-2
Original file line numberDiff line numberDiff line change
@@ -157,8 +157,8 @@ cdef _wrap_timedelta_result(result):
157157
"""
158158
if PyDelta_Check(result):
159159
# convert Timedelta back to a Tick
160-
from pandas.tseries.offsets import _delta_to_tick
161-
return _delta_to_tick(result)
160+
from pandas.tseries.offsets import delta_to_tick
161+
return delta_to_tick(result)
162162

163163
return result
164164

pandas/core/arrays/period.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
import pandas.core.common as com
4040

4141
from pandas.tseries import frequencies
42-
from pandas.tseries.offsets import DateOffset, Tick, _delta_to_tick
42+
from pandas.tseries.offsets import DateOffset, Tick, delta_to_tick
4343

4444

4545
def _field_accessor(name: str, alias: int, docstring=None):
@@ -487,7 +487,7 @@ def _time_shift(self, periods, freq=None):
487487
def _box_func(self):
488488
return lambda x: Period._from_ordinal(ordinal=x, freq=self.freq)
489489

490-
def asfreq(self, freq=None, how="E") -> "PeriodArray":
490+
def asfreq(self, freq=None, how: str = "E") -> "PeriodArray":
491491
"""
492492
Convert the Period Array/Index to the specified frequency `freq`.
493493
@@ -759,7 +759,7 @@ def raise_on_incompatible(left, right):
759759
elif isinstance(right, (ABCPeriodIndex, PeriodArray, Period, DateOffset)):
760760
other_freq = right.freqstr
761761
else:
762-
other_freq = _delta_to_tick(Timedelta(right)).freqstr
762+
other_freq = delta_to_tick(Timedelta(right)).freqstr
763763

764764
msg = DIFFERENT_FREQ.format(
765765
cls=type(left).__name__, own_freq=left.freqstr, other_freq=other_freq

pandas/core/indexes/accessors.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -428,7 +428,6 @@ def __new__(cls, data: "Series"):
428428
# we need to choose which parent (datetime or timedelta) is
429429
# appropriate. Since we're checking the dtypes anyway, we'll just
430430
# do all the validation here.
431-
from pandas import Series
432431

433432
if not isinstance(data, ABCSeries):
434433
raise TypeError(
@@ -437,7 +436,7 @@ def __new__(cls, data: "Series"):
437436

438437
orig = data if is_categorical_dtype(data) else None
439438
if orig is not None:
440-
data = Series(
439+
data = data._constructor(
441440
orig.array,
442441
name=orig.name,
443442
copy=False,

pandas/core/indexes/period.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,8 @@
66

77
from pandas._libs import index as libindex
88
from pandas._libs.lib import no_default
9-
from pandas._libs.tslibs import frequencies as libfrequencies, resolution
9+
from pandas._libs.tslibs import Period, frequencies as libfrequencies, resolution
1010
from pandas._libs.tslibs.parsing import parse_time_string
11-
from pandas._libs.tslibs.period import Period
1211
from pandas._typing import DtypeObj, Label
1312
from pandas.util._decorators import Appender, cache_readonly, doc
1413

pandas/core/indexes/timedeltas.py

+1-10
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,7 @@
2929

3030

3131
@inherit_names(
32-
[
33-
"_box_values",
34-
"__neg__",
35-
"__pos__",
36-
"__abs__",
37-
"total_seconds",
38-
"round",
39-
"floor",
40-
"ceil",
41-
]
32+
["__neg__", "__pos__", "__abs__", "total_seconds", "round", "floor", "ceil"]
4233
+ TimedeltaArray._field_ops,
4334
TimedeltaArray,
4435
wrap=True,

pandas/core/internals/blocks.py

+5-4
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ def get_block_values_for_json(self) -> np.ndarray:
216216
"""
217217
This is used in the JSON C code.
218218
"""
219-
# TODO(2DEA): reshape will be unnecessary with 2D EAs
219+
# TODO(EA2D): reshape will be unnecessary with 2D EAs
220220
return np.asarray(self.values).reshape(self.shape)
221221

222222
@property
@@ -341,6 +341,7 @@ def apply(self, func, **kwargs) -> List["Block"]:
341341
def _split_op_result(self, result) -> List["Block"]:
342342
# See also: split_and_operate
343343
if is_extension_array_dtype(result) and result.ndim > 1:
344+
# TODO(EA2D): unnecessary with 2D EAs
344345
# if we get a 2D ExtensionArray, we need to split it into 1D pieces
345346
nbs = []
346347
for i, loc in enumerate(self.mgr_locs):
@@ -1548,7 +1549,7 @@ def __init__(self, values, placement, ndim=None):
15481549
super().__init__(values, placement, ndim=ndim)
15491550

15501551
if self.ndim == 2 and len(self.mgr_locs) != 1:
1551-
# TODO(2DEA): check unnecessary with 2D EAs
1552+
# TODO(EA2D): check unnecessary with 2D EAs
15521553
raise AssertionError("block.size != values.size")
15531554

15541555
@property
@@ -2273,7 +2274,7 @@ def equals(self, other) -> bool:
22732274
def quantile(self, qs, interpolation="linear", axis=0):
22742275
naive = self.values.view("M8[ns]")
22752276

2276-
# kludge for 2D block with 1D values
2277+
# TODO(EA2D): kludge for 2D block with 1D values
22772278
naive = naive.reshape(self.shape)
22782279

22792280
blk = self.make_block(naive)
@@ -2398,7 +2399,7 @@ def f(mask, val, idx):
23982399
copy=copy,
23992400
)
24002401
if isinstance(values, np.ndarray):
2401-
# TODO: allow EA once reshape is supported
2402+
# TODO(EA2D): allow EA once reshape is supported
24022403
values = values.reshape(shape)
24032404

24042405
return values

pandas/core/internals/concat.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
import numpy as np
77

8-
from pandas._libs import internals as libinternals, tslibs
8+
from pandas._libs import NaT, internals as libinternals
99
from pandas.util._decorators import cache_readonly
1010

1111
from pandas.core.dtypes.cast import maybe_promote
@@ -406,7 +406,7 @@ def _get_empty_dtype_and_na(join_units):
406406
# GH-25014. We use NaT instead of iNaT, since this eventually
407407
# ends up in DatetimeArray.take, which does not allow iNaT.
408408
dtype = upcast_classes["datetimetz"]
409-
return dtype[0], tslibs.NaT
409+
return dtype[0], NaT
410410
elif "datetime" in upcast_classes:
411411
return np.dtype("M8[ns]"), np.datetime64("NaT", "ns")
412412
elif "timedelta" in upcast_classes:

pandas/core/resample.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ def _gotitem(self, key, ndim: int, subset=None):
323323
Parameters
324324
----------
325325
key : string / list of selections
326-
ndim : 1,2
326+
ndim : {1, 2}
327327
requested ndim of result
328328
subset : object, default None
329329
subset to act on

pandas/tests/arithmetic/test_timedelta64.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,9 @@ def test_subtraction_ops(self):
313313
tm.assert_index_equal(result, expected, check_names=False)
314314

315315
result = dti - td
316-
expected = DatetimeIndex(["20121231", "20130101", "20130102"], name="bar")
316+
expected = DatetimeIndex(
317+
["20121231", "20130101", "20130102"], freq="D", name="bar"
318+
)
317319
tm.assert_index_equal(result, expected, check_names=False)
318320

319321
result = dt - tdi
@@ -401,7 +403,9 @@ def _check(result, expected):
401403
_check(result, expected)
402404

403405
result = dti_tz - td
404-
expected = DatetimeIndex(["20121231", "20130101", "20130102"], tz="US/Eastern")
406+
expected = DatetimeIndex(
407+
["20121231", "20130101", "20130102"], tz="US/Eastern", freq="D"
408+
)
405409
tm.assert_index_equal(result, expected)
406410

407411
def test_dti_tdi_numeric_ops(self):

pandas/tests/series/methods/test_replace.py

+1-5
Original file line numberDiff line numberDiff line change
@@ -186,11 +186,7 @@ def check_replace(to_rep, val, expected):
186186
check_replace(tr, v, e)
187187

188188
# test an object with dates + floats + integers + strings
189-
dr = (
190-
pd.date_range("1/1/2001", "1/10/2001", freq="D")
191-
.to_series()
192-
.reset_index(drop=True)
193-
)
189+
dr = pd.Series(pd.date_range("1/1/2001", "1/10/2001", freq="D"))
194190
result = dr.astype(object).replace([dr[0], dr[1], dr[2]], [1.0, 2, "a"])
195191
expected = pd.Series([1.0, 2, "a"] + dr[3:].tolist(), dtype=object)
196192
tm.assert_series_equal(result, expected)

pandas/tests/tseries/offsets/test_ticks.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,11 @@ def test_apply_ticks():
3333
def test_delta_to_tick():
3434
delta = timedelta(3)
3535

36-
tick = offsets._delta_to_tick(delta)
36+
tick = offsets.delta_to_tick(delta)
3737
assert tick == offsets.Day(3)
3838

3939
td = Timedelta(nanoseconds=5)
40-
tick = offsets._delta_to_tick(td)
40+
tick = offsets.delta_to_tick(td)
4141
assert tick == Nano(5)
4242

4343

pandas/tseries/offsets.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -2548,7 +2548,7 @@ def __add__(self, other):
25482548
if type(self) == type(other):
25492549
return type(self)(self.n + other.n)
25502550
else:
2551-
return _delta_to_tick(self.delta + other.delta)
2551+
return delta_to_tick(self.delta + other.delta)
25522552
elif isinstance(other, Period):
25532553
return other + self
25542554
try:
@@ -2635,7 +2635,7 @@ def is_anchored(self) -> bool:
26352635
return False
26362636

26372637

2638-
def _delta_to_tick(delta: timedelta) -> Tick:
2638+
def delta_to_tick(delta: timedelta) -> Tick:
26392639
if delta.microseconds == 0 and getattr(delta, "nanoseconds", 0) == 0:
26402640
# nanoseconds only for pd.Timedelta
26412641
if delta.seconds == 0:

0 commit comments

Comments
 (0)