diff --git a/pandas/_testing/__init__.py b/pandas/_testing/__init__.py index f5f2aa23459e4..25da4f8eb8278 100644 --- a/pandas/_testing/__init__.py +++ b/pandas/_testing/__init__.py @@ -296,7 +296,7 @@ def box_expected(expected, box_cls, transpose: bool = True): # pd.array would return an IntegerArray expected = PandasArray(np.asarray(expected._values)) else: - expected = pd.array(expected) + expected = pd.array(expected, copy=False) elif box_cls is Index: expected = Index._with_infer(expected) elif box_cls is Series: diff --git a/pandas/core/arrays/datetimelike.py b/pandas/core/arrays/datetimelike.py index 83c7fa2fbdca1..500adb316adf0 100644 --- a/pandas/core/arrays/datetimelike.py +++ b/pandas/core/arrays/datetimelike.py @@ -1259,8 +1259,8 @@ def _add_timedeltalike_scalar(self, other): return type(self)._simple_new(new_values, dtype=self.dtype) # PeriodArray overrides, so we only get here with DTA/TDA - # error: "DatetimeLikeArrayMixin" has no attribute "_reso" - inc = delta_to_nanoseconds(other, reso=self._reso) # type: ignore[attr-defined] + self = cast("DatetimeArray | TimedeltaArray", self) + inc = delta_to_nanoseconds(other, reso=self._reso) new_values = checked_add_with_arr(self.asi8, inc, arr_mask=self._isnan) new_values = new_values.view(self._ndarray.dtype) @@ -1270,10 +1270,7 @@ def _add_timedeltalike_scalar(self, other): # adding a scalar preserves freq new_freq = self.freq - # error: Unexpected keyword argument "freq" for "_simple_new" of "NDArrayBacked" - return type(self)._simple_new( # type: ignore[call-arg] - new_values, dtype=self.dtype, freq=new_freq - ) + return type(self)._simple_new(new_values, dtype=self.dtype, freq=new_freq) def _add_timedelta_arraylike( self, other: TimedeltaArray | npt.NDArray[np.timedelta64] @@ -2217,11 +2214,11 @@ def validate_periods(periods: None) -> None: @overload -def validate_periods(periods: float) -> int: +def validate_periods(periods: int | float) -> int: ... -def validate_periods(periods: float | None) -> int | None: +def validate_periods(periods: int | float | None) -> int | None: """ If a `periods` argument is passed to the Datetime/Timedelta Array/Index constructor, cast it to an integer. @@ -2244,9 +2241,8 @@ def validate_periods(periods: float | None) -> int | None: periods = int(periods) elif not lib.is_integer(periods): raise TypeError(f"periods must be a number, got {periods}") - # error: Incompatible return value type (got "Optional[float]", - # expected "Optional[int]") - return periods # type: ignore[return-value] + periods = cast(int, periods) + return periods def validate_inferred_freq( diff --git a/pandas/core/dtypes/cast.py b/pandas/core/dtypes/cast.py index 75a0db3233130..966eca9cc7952 100644 --- a/pandas/core/dtypes/cast.py +++ b/pandas/core/dtypes/cast.py @@ -220,7 +220,7 @@ def _maybe_unbox_datetimelike(value: Scalar, dtype: DtypeObj) -> Scalar: """ if is_valid_na_for_dtype(value, dtype): # GH#36541: can't fill array directly with pd.NaT - # > np.empty(10, dtype="datetime64[64]").fill(pd.NaT) + # > np.empty(10, dtype="datetime64[ns]").fill(pd.NaT) # ValueError: cannot convert float NaN to integer value = dtype.type("NaT", "ns") elif isinstance(value, Timestamp): @@ -800,6 +800,8 @@ def infer_dtype_from_scalar(val, pandas_dtype: bool = False) -> tuple[DtypeObj, if val is NaT or val.tz is None: # type: ignore[comparison-overlap] dtype = np.dtype("M8[ns]") val = val.to_datetime64() + # TODO(2.0): this should be dtype = val.dtype + # to get the correct M8 resolution else: if pandas_dtype: dtype = DatetimeTZDtype(unit="ns", tz=val.tz)