Skip to content

Commit f1ed6de

Browse files
authored
CLN: small cleanups (#48836)
1 parent 5a9de8b commit f1ed6de

File tree

3 files changed

+11
-13
lines changed

3 files changed

+11
-13
lines changed

pandas/_testing/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ def box_expected(expected, box_cls, transpose: bool = True):
296296
# pd.array would return an IntegerArray
297297
expected = PandasArray(np.asarray(expected._values))
298298
else:
299-
expected = pd.array(expected)
299+
expected = pd.array(expected, copy=False)
300300
elif box_cls is Index:
301301
expected = Index._with_infer(expected)
302302
elif box_cls is Series:

pandas/core/arrays/datetimelike.py

+7-11
Original file line numberDiff line numberDiff line change
@@ -1295,8 +1295,8 @@ def _add_timedeltalike_scalar(self, other):
12951295
return type(self)._simple_new(new_values, dtype=self.dtype)
12961296

12971297
# PeriodArray overrides, so we only get here with DTA/TDA
1298-
# error: "DatetimeLikeArrayMixin" has no attribute "_reso"
1299-
inc = delta_to_nanoseconds(other, reso=self._reso) # type: ignore[attr-defined]
1298+
self = cast("DatetimeArray | TimedeltaArray", self)
1299+
inc = delta_to_nanoseconds(other, reso=self._reso)
13001300

13011301
new_values = checked_add_with_arr(self.asi8, inc, arr_mask=self._isnan)
13021302
new_values = new_values.view(self._ndarray.dtype)
@@ -1306,10 +1306,7 @@ def _add_timedeltalike_scalar(self, other):
13061306
# adding a scalar preserves freq
13071307
new_freq = self.freq
13081308

1309-
# error: Unexpected keyword argument "freq" for "_simple_new" of "NDArrayBacked"
1310-
return type(self)._simple_new( # type: ignore[call-arg]
1311-
new_values, dtype=self.dtype, freq=new_freq
1312-
)
1309+
return type(self)._simple_new(new_values, dtype=self.dtype, freq=new_freq)
13131310

13141311
def _add_timedelta_arraylike(
13151312
self, other: TimedeltaArray | npt.NDArray[np.timedelta64]
@@ -2264,11 +2261,11 @@ def validate_periods(periods: None) -> None:
22642261

22652262

22662263
@overload
2267-
def validate_periods(periods: float) -> int:
2264+
def validate_periods(periods: int | float) -> int:
22682265
...
22692266

22702267

2271-
def validate_periods(periods: float | None) -> int | None:
2268+
def validate_periods(periods: int | float | None) -> int | None:
22722269
"""
22732270
If a `periods` argument is passed to the Datetime/Timedelta Array/Index
22742271
constructor, cast it to an integer.
@@ -2291,9 +2288,8 @@ def validate_periods(periods: float | None) -> int | None:
22912288
periods = int(periods)
22922289
elif not lib.is_integer(periods):
22932290
raise TypeError(f"periods must be a number, got {periods}")
2294-
# error: Incompatible return value type (got "Optional[float]",
2295-
# expected "Optional[int]")
2296-
return periods # type: ignore[return-value]
2291+
periods = cast(int, periods)
2292+
return periods
22972293

22982294

22992295
def validate_inferred_freq(

pandas/core/dtypes/cast.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ def _maybe_unbox_datetimelike(value: Scalar, dtype: DtypeObj) -> Scalar:
220220
"""
221221
if is_valid_na_for_dtype(value, dtype):
222222
# GH#36541: can't fill array directly with pd.NaT
223-
# > np.empty(10, dtype="datetime64[64]").fill(pd.NaT)
223+
# > np.empty(10, dtype="datetime64[ns]").fill(pd.NaT)
224224
# ValueError: cannot convert float NaN to integer
225225
value = dtype.type("NaT", "ns")
226226
elif isinstance(value, Timestamp):
@@ -800,6 +800,8 @@ def infer_dtype_from_scalar(val, pandas_dtype: bool = False) -> tuple[DtypeObj,
800800
if val is NaT or val.tz is None: # type: ignore[comparison-overlap]
801801
dtype = np.dtype("M8[ns]")
802802
val = val.to_datetime64()
803+
# TODO(2.0): this should be dtype = val.dtype
804+
# to get the correct M8 resolution
803805
else:
804806
if pandas_dtype:
805807
dtype = DatetimeTZDtype(unit="ns", tz=val.tz)

0 commit comments

Comments
 (0)