Skip to content

Commit ad78190

Browse files
committed
REF (feedback): move maybe_box_datetimelike common.py -> dtypes/cast.py
1 parent 717a6e3 commit ad78190

File tree

6 files changed

+31
-30
lines changed

6 files changed

+31
-30
lines changed

pandas/core/arrays/sparse/array.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
construct_1d_arraylike_from_scalar,
2323
find_common_type,
2424
infer_dtype_from_scalar,
25+
maybe_box_datetimelike,
2526
)
2627
from pandas.core.dtypes.common import (
2728
is_array_like,
@@ -815,7 +816,7 @@ def _get_val_at(self, loc):
815816
return self.fill_value
816817
else:
817818
val = self.sp_values[sp_loc]
818-
val = com.maybe_box_datetimelike(val, self.sp_values.dtype)
819+
val = maybe_box_datetimelike(val, self.sp_values.dtype)
819820
return val
820821

821822
def take(self, indices, allow_fill=False, fill_value=None) -> "SparseArray":

pandas/core/common.py

+5-20
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,21 @@
66

77
from collections import abc, defaultdict
88
import contextlib
9-
from datetime import datetime, timedelta
109
from functools import partial
1110
import inspect
1211
from typing import Any, Collection, Iterable, Iterator, List, Union, cast
1312
import warnings
1413

1514
import numpy as np
1615

17-
from pandas._libs import lib, tslibs
16+
from pandas._libs import lib
1817
from pandas._typing import AnyArrayLike, Scalar, T
1918
from pandas.compat.numpy import np_version_under1p18
2019

21-
from pandas.core.dtypes.cast import construct_1d_object_array_from_listlike
20+
from pandas.core.dtypes.cast import (
21+
construct_1d_object_array_from_listlike,
22+
maybe_box_datetimelike,
23+
)
2224
from pandas.core.dtypes.common import (
2325
is_array_like,
2426
is_bool_dtype,
@@ -78,23 +80,6 @@ def consensus_name_attr(objs):
7880
return name
7981

8082

81-
def maybe_box_datetimelike(value, dtype=None, native=False):
82-
# turn a datetime like into a Timestamp/timedelta as needed
83-
if dtype == object:
84-
# If we dont have datetime64/timedelta64 dtype, we dont want to
85-
# box datetimelike scalars
86-
return value
87-
88-
if isinstance(value, (np.datetime64, datetime)):
89-
ts = tslibs.Timestamp(value)
90-
value = ts.to_pydatetime() if native else ts
91-
elif isinstance(value, (np.timedelta64, timedelta)):
92-
td = tslibs.Timedelta(value)
93-
value = td.to_pydatetime() if native else td
94-
95-
return value
96-
97-
9883
def is_bool_indexer(key: Any) -> bool:
9984
"""
10085
Check whether `key` is a valid boolean indexer.

pandas/core/dtypes/cast.py

+15
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,21 @@ def is_nested_object(obj) -> bool:
134134
return False
135135

136136

137+
def maybe_box_datetimelike(value, dtype=None):
138+
# turn a datetime like into a Timestamp/timedelta as needed
139+
if dtype == object:
140+
# If we dont have datetime64/timedelta64 dtype, we dont want to
141+
# box datetimelike scalars
142+
return value
143+
144+
if isinstance(value, (np.datetime64, datetime)):
145+
value = tslib.Timestamp(value)
146+
elif isinstance(value, (np.timedelta64, timedelta)):
147+
value = tslib.Timedelta(value)
148+
149+
return value
150+
151+
137152
def maybe_downcast_to_dtype(result, dtype: Union[str, np.dtype]):
138153
"""
139154
try to cast to the specified dtype (e.g. convert back to bool/int

pandas/core/frame.py

+4-6
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@
8383
find_common_type,
8484
infer_dtype_from_scalar,
8585
invalidate_string_dtypes,
86+
maybe_box_datetimelike,
8687
maybe_cast_to_datetime,
8788
maybe_casted_values,
8889
maybe_convert_platform,
@@ -1527,15 +1528,15 @@ def to_dict(self, orient="dict", into=dict):
15271528
(
15281529
"data",
15291530
[
1530-
list(map(com.maybe_box_datetimelike, t))
1531+
list(map(maybe_box_datetimelike, t))
15311532
for t in self.itertuples(index=False, name=None)
15321533
],
15331534
),
15341535
)
15351536
)
15361537

15371538
elif orient == "series":
1538-
return into_c((k, com.maybe_box_datetimelike(v)) for k, v in self.items())
1539+
return into_c((k, maybe_box_datetimelike(v)) for k, v in self.items())
15391540

15401541
elif orient == "records":
15411542
columns = self.columns.tolist()
@@ -1544,10 +1545,7 @@ def to_dict(self, orient="dict", into=dict):
15441545
for row in self.itertuples(index=False, name=None)
15451546
)
15461547
return [
1547-
into_c(
1548-
(k, com.maybe_box_datetimelike(v, native=True))
1549-
for k, v in row.items()
1550-
)
1548+
into_c((k, maybe_box_datetimelike(v)) for k, v in row.items())
15511549
for row in rows
15521550
]
15531551

pandas/core/indexes/interval.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
is_number,
3737
is_object_dtype,
3838
is_scalar,
39+
maybe_box_datetimelike,
3940
)
4041

4142
from pandas.core.algorithms import take_1d
@@ -1206,8 +1207,8 @@ def interval_range(
12061207
IntervalIndex([[1, 2], [2, 3], [3, 4], [4, 5]],
12071208
closed='both', dtype='interval[int64]')
12081209
"""
1209-
start = com.maybe_box_datetimelike(start)
1210-
end = com.maybe_box_datetimelike(end)
1210+
start = maybe_box_datetimelike(start)
1211+
end = maybe_box_datetimelike(end)
12111212
endpoint = start if start is not None else end
12121213

12131214
if freq is None and com.any_none(periods, start, end):

pandas/core/internals/blocks.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
find_common_type,
2020
infer_dtype_from,
2121
infer_dtype_from_scalar,
22+
maybe_box_datetimelike,
2223
maybe_downcast_numeric,
2324
maybe_downcast_to_dtype,
2425
maybe_infer_dtype_type,
@@ -843,7 +844,7 @@ def comp(s: Scalar, mask: np.ndarray, regex: bool = False) -> np.ndarray:
843844
if isna(s):
844845
return ~mask
845846

846-
s = com.maybe_box_datetimelike(s)
847+
s = maybe_box_datetimelike(s)
847848
return compare_or_regex_search(self.values, s, regex, mask)
848849

849850
# Calculate the mask once, prior to the call of comp

0 commit comments

Comments
 (0)