Skip to content

API: integer values and non-nano dtype #51092

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Feb 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v2.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -628,6 +628,7 @@ Other API changes
- Passing ``dtype`` of "timedelta64[s]", "timedelta64[ms]", or "timedelta64[us]" to :class:`TimedeltaIndex`, :class:`Series`, or :class:`DataFrame` constructors will now retain that dtype instead of casting to "timedelta64[ns]"; passing a dtype with lower resolution for :class:`Series` or :class:`DataFrame` will be cast to the lowest supported resolution "timedelta64[s]" (:issue:`49014`)
- Passing a ``np.datetime64`` object with non-nanosecond resolution to :class:`Timestamp` will retain the input resolution if it is "s", "ms", "us", or "ns"; otherwise it will be cast to the closest supported resolution (:issue:`49008`)
- Passing ``datetime64`` values with resolution other than nanosecond to :func:`to_datetime` will retain the input resolution if it is "s", "ms", "us", or "ns"; otherwise it will be cast to the closest supported resolution (:issue:`50369`)
- Passing integer values and a non-nanosecond datetime64 dtype (e.g. "datetime64[s]") :class:`DataFrame`, :class:`Series`, or :class:`Index` will treat the values as multiples of the dtype's unit, matching the behavior of e.g. ``Series(np.array(values, dtype="M8[s]"))`` (:issue:`51092`)
- Passing a string in ISO-8601 format to :class:`Timestamp` will retain the resolution of the parsed input if it is "s", "ms", "us", or "ns"; otherwise it will be cast to the closest supported resolution (:issue:`49737`)
- The ``other`` argument in :meth:`DataFrame.mask` and :meth:`Series.mask` now defaults to ``no_default`` instead of ``np.nan`` consistent with :meth:`DataFrame.where` and :meth:`Series.where`. Entries will be filled with the corresponding NULL value (``np.nan`` for numpy dtypes, ``pd.NA`` for extension dtypes). (:issue:`49111`)
- Changed behavior of :meth:`Series.quantile` and :meth:`DataFrame.quantile` with :class:`SparseDtype` to retain sparse dtype (:issue:`49583`)
Expand Down
12 changes: 10 additions & 2 deletions pandas/core/arrays/datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,7 @@ def _from_sequence_not_strict(
dayfirst=dayfirst,
yearfirst=yearfirst,
ambiguous=ambiguous,
out_unit=unit,
)
# We have to call this again after possibly inferring a tz above
_validate_tz_from_dtype(dtype, tz, explicit_tz_none)
Expand Down Expand Up @@ -1966,6 +1967,7 @@ def _sequence_to_dt64ns(
dayfirst: bool = False,
yearfirst: bool = False,
ambiguous: TimeAmbiguous = "raise",
out_unit: str | None = None,
):
"""
Parameters
Expand All @@ -1977,6 +1979,8 @@ def _sequence_to_dt64ns(
yearfirst : bool, default False
ambiguous : str, bool, or arraylike, default 'raise'
See pandas._libs.tslibs.tzconversion.tz_localize_to_utc.
out_unit : str or None, default None
Desired output resolution.

Returns
-------
Expand Down Expand Up @@ -2004,6 +2008,10 @@ def _sequence_to_dt64ns(
data, copy = maybe_convert_dtype(data, copy, tz=tz)
data_dtype = getattr(data, "dtype", None)

out_dtype = DT64NS_DTYPE
if out_unit is not None:
out_dtype = np.dtype(f"M8[{out_unit}]")

if (
is_object_dtype(data_dtype)
or is_string_dtype(data_dtype)
Expand Down Expand Up @@ -2090,7 +2098,7 @@ def _sequence_to_dt64ns(
# assume this data are epoch timestamps
if data.dtype != INT64_DTYPE:
data = data.astype(np.int64, copy=False)
result = data.view(DT64NS_DTYPE)
result = data.view(out_dtype)

if copy:
result = result.copy()
Expand Down Expand Up @@ -2203,7 +2211,7 @@ def maybe_convert_dtype(data, copy: bool, tz: tzinfo | None = None):
# GH#23675, GH#45573 deprecated to treat symmetrically with integer dtypes.
# Note: data.astype(np.int64) fails ARM tests, see
# https://github.com/pandas-dev/pandas/issues/49468.
data = data.astype("M8[ns]").view("i8")
data = data.astype(DT64NS_DTYPE).view("i8")
copy = False

elif is_timedelta64_dtype(data.dtype) or is_bool_dtype(data.dtype):
Expand Down
10 changes: 10 additions & 0 deletions pandas/tests/series/test_constructors.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,16 @@


class TestSeriesConstructors:
def test_from_ints_with_non_nano_dt64_dtype(self, index_or_series):
values = np.arange(10)

res = index_or_series(values, dtype="M8[s]")
expected = index_or_series(values.astype("M8[s]"))
tm.assert_equal(res, expected)

res = index_or_series(list(values), dtype="M8[s]")
tm.assert_equal(res, expected)

def test_from_na_value_and_interval_of_datetime_dtype(self):
# GH#41805
ser = Series([None], dtype="interval[datetime64[ns]]")
Expand Down