Skip to content

Commit 5164f2f

Browse files
committed
API: Use lowest possible datetime64 resolution in PeriodIndex.to_timestamp
1 parent 51599e7 commit 5164f2f

File tree

2 files changed

+6
-25
lines changed

2 files changed

+6
-25
lines changed

pandas/core/arrays/period.py

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -824,24 +824,7 @@ def to_timestamp(self, freq=None, how: str = "start") -> DatetimeArray:
824824

825825
new_parr = self.asfreq(freq, how=how)
826826

827-
# Map period frequency to appropriate datetime64 resolution
828-
freq_to_unit = {
829-
"A": "Y",
830-
"Y": "Y",
831-
"Q": "M",
832-
"M": "M",
833-
"W": "D",
834-
"D": "D",
835-
"H": "h",
836-
"T": "m",
837-
"S": "s",
838-
"L": "ms",
839-
"U": "us",
840-
"N": "ns",
841-
}
842-
rule_code = getattr(freq, "rule_code", None)
843-
unit = freq_to_unit.get(rule_code, "ns")
844-
dt64_dtype = np.dtype(f"datetime64[{unit}]")
827+
dt64_dtype = np.dtype("datetime64[ns]")
845828

846829
new_data = libperiod.periodarr_to_dt64arr(new_parr.asi8, base)
847830
dta = DatetimeArray._from_sequence(new_data, dtype=dt64_dtype)

pandas/tests/indexes/period/test_period.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -236,33 +236,31 @@ def test_dunder_array(array):
236236
def test_to_timestamp_monthly_resolution():
237237
idx = PeriodIndex(["2011-01", "NaT", "2011-02"], freq="2M")
238238
ts = idx.to_timestamp()
239-
assert ts.dtype == np.dtype("datetime64[M]")
239+
assert ts.dtype == np.dtype("datetime64[ns]")
240240

241241

242242
def test_to_timestamp_yearly_resolution():
243243
idx = PeriodIndex(["2011", "2012"], freq="A")
244244
ts = idx.to_timestamp()
245-
assert ts.dtype == np.dtype("datetime64[Y]")
245+
assert ts.dtype == np.dtype("datetime64[ns]")
246246

247247

248248
def test_to_timestamp_large_month_no_out_of_bounds():
249249
idx = PeriodIndex(["May 3000"], freq="M")
250250
ts = idx.to_timestamp()
251-
assert ts.dtype == np.dtype("datetime64[M]")
252-
assert str(ts[0]) == "3000-05"
251+
assert ts.dtype == np.dtype("datetime64[ns]")
253252

254253

255254
def test_to_timestamp_large_year_no_out_of_bounds():
256255
idx = PeriodIndex(["3000"], freq="Y")
257256
ts = idx.to_timestamp()
258-
assert ts.dtype == np.dtype("datetime64[Y]")
259-
assert str(ts[0]) == "3000"
257+
assert ts.dtype == np.dtype("datetime64[ns]")
260258

261259

262260
def test_to_timestamp_daily_resolution():
263261
idx = PeriodIndex(["2011-01-01", "2011-01-02"], freq="D")
264262
ts = idx.to_timestamp()
265-
assert ts.dtype == np.dtype("datetime64[D]")
263+
assert ts.dtype == np.dtype("datetime64[ns]")
266264

267265

268266
def test_to_timestamp_nanosecond_resolution():

0 commit comments

Comments
 (0)