Skip to content

Commit f925f83

Browse files
committed
CLN: switch to generic functions
Stop relying on inner implementations of IntegerArray and numpy arrays. Use public funcitons as much as possible.
1 parent 65f5c41 commit f925f83

File tree

2 files changed

+10
-7
lines changed

2 files changed

+10
-7
lines changed

pandas/core/tools/datetimes.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -318,17 +318,19 @@ def _convert_listlike_datetimes(
318318
elif unit is not None:
319319
if format is not None:
320320
raise ValueError("cannot specify both format and unit")
321-
arg = getattr(arg, "values", arg)
321+
arg = getattr(arg, "_values", arg)
322322
# GH 30050 pass an ndarray to tslib.array_with_unit_to_datetime
323323
# because it expects an ndarray argument
324324
if isinstance(arg, IntegerArray):
325-
arg_np = np.array(arg[np.logical_not(arg._mask)], dtype=type(arg[0]))
325+
# Send only non-na values to array_with_unit_to_datetime
326+
mask_na = arg.isna()
326327
result_np, tz_parsed = tslib.array_with_unit_to_datetime(
327-
arg_np, unit, errors=errors
328+
np.compress(np.logical_not(mask_na), arg), unit, errors=errors
328329
)
329-
result = np.empty(arg.shape[0], dtype="datetime64[" + unit + "]")
330-
result[arg._mask] = np.datetime64("nat")
331-
result[np.logical_not(arg._mask)] = result_np
330+
# Insert na values back in proper positions
331+
ins_index = np.ravel(np.argwhere(mask_na))
332+
ins_index -= range(ins_index.shape[0])
333+
result = np.insert(result_np, ins_index, None)
332334
else:
333335
result, tz_parsed = tslib.array_with_unit_to_datetime(
334336
arg, unit, errors=errors

pandas/tests/indexes/datetimes/test_tools.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2292,6 +2292,7 @@ def test_should_cache_errors(unique_share, check_count, err_message):
22922292
with pytest.raises(AssertionError, match=err_message):
22932293
tools.should_cache(arg, unique_share, check_count)
22942294

2295+
22952296
def test_intarray_to_datetime():
22962297
# Test for #30050
22972298
ser = pd.Series([1, 2, None, 2 ** 61, None])
@@ -2308,4 +2309,4 @@ def test_intarray_to_datetime():
23082309
np.datetime64("NaT"),
23092310
]
23102311
)
2311-
tm.assert_series_equal(res, expected)
2312+
tm.assert_series_equal(res, expected)

0 commit comments

Comments
 (0)