Skip to content

Commit ff7b7e6

Browse files
committed
Return Int64 dataframe when NaT present
1 parent f0d1ae6 commit ff7b7e6

File tree

2 files changed

+9
-10
lines changed

2 files changed

+9
-10
lines changed

pandas/core/arrays/datetimes.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1271,12 +1271,11 @@ def isocalendar(self):
12711271
from pandas import DataFrame
12721272

12731273
sarray = fields.build_isocalendar_sarray(self.asi8)
1274-
iso_calendar_array = self._maybe_mask_results(
1275-
sarray,
1276-
fill_value=(np.nan, np.nan, np.nan),
1277-
convert=[(n, "<f8") for n in sarray.dtype.names],
1278-
)
1279-
return DataFrame(iso_calendar_array, columns=["year", "week", "day"])
1274+
iso_calendar_df = DataFrame(sarray, columns=["year", "week", "day"])
1275+
if self._hasnans:
1276+
iso_calendar_df = iso_calendar_df.astype("Int64")
1277+
iso_calendar_df.iloc[self._isnan] = None
1278+
return iso_calendar_df
12801279

12811280
year = _field_accessor(
12821281
"year",

pandas/tests/series/test_datetime_values.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -672,18 +672,18 @@ def test_setitem_with_different_tz(self):
672672
"input_series, expected_output, expected_type",
673673
[
674674
[["2020-01-01"], [[2020, 1, 3]], "int32"],
675-
[[pd.NaT], [[np.NaN, np.NaN, np.NaN]], "float64"],
675+
[[pd.NaT], [[np.NaN, np.NaN, np.NaN]], "Int64"],
676676
[["2019-12-31", "2019-12-29"], [[2020, 1, 2], [2019, 52, 7]], "int32"],
677677
[
678678
["2010-01-01", pd.NaT],
679679
[[2009, 53, 5], [np.NaN, np.NaN, np.NaN]],
680-
"float64",
680+
"Int64",
681681
],
682682
],
683683
)
684684
def test_isocalendar(self, input_series, expected_output, expected_type):
685-
ser = pd.to_datetime(pd.Series(input_series))
685+
result = pd.to_datetime(pd.Series(input_series)).dt.isocalendar
686686
expected_frame = pd.DataFrame(
687687
expected_output, columns=["year", "week", "day"]
688688
).astype(expected_type)
689-
tm.assert_frame_equal(ser.dt.isocalendar, expected_frame)
689+
tm.assert_frame_equal(result, expected_frame)

0 commit comments

Comments
 (0)