Skip to content

Commit f7b868c

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 616416d commit f7b868c

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
@@ -316,17 +316,19 @@ def _convert_listlike_datetimes(
316316
elif unit is not None:
317317
if format is not None:
318318
raise ValueError("cannot specify both format and unit")
319-
arg = getattr(arg, "values", arg)
319+
arg = getattr(arg, "_values", arg)
320320
# GH 30050 pass an ndarray to tslib.array_with_unit_to_datetime
321321
# because it expects an ndarray argument
322322
if isinstance(arg, IntegerArray):
323-
arg_np = np.array(arg[np.logical_not(arg._mask)], dtype=type(arg[0]))
323+
# Send only non-na values to array_with_unit_to_datetime
324+
mask_na = arg.isna()
324325
result_np, tz_parsed = tslib.array_with_unit_to_datetime(
325-
arg_np, unit, errors=errors
326+
np.compress(np.logical_not(mask_na), arg), unit, errors=errors
326327
)
327-
result = np.empty(arg.shape[0], dtype="datetime64[" + unit + "]")
328-
result[arg._mask] = np.datetime64("nat")
329-
result[np.logical_not(arg._mask)] = result_np
328+
# Insert na values back in proper positions
329+
ins_index = np.ravel(np.argwhere(mask_na))
330+
ins_index -= range(ins_index.shape[0])
331+
result = np.insert(result_np, ins_index, None)
330332
else:
331333
result, tz_parsed = tslib.array_with_unit_to_datetime(
332334
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
@@ -2275,6 +2275,7 @@ def test_should_cache_errors(unique_share, check_count, err_message):
22752275
with pytest.raises(AssertionError, match=err_message):
22762276
tools.should_cache(arg, unique_share, check_count)
22772277

2278+
22782279
def test_intarray_to_datetime():
22792280
# Test for #30050
22802281
ser = pd.Series([1, 2, None, 2 ** 61, None])
@@ -2291,4 +2292,4 @@ def test_intarray_to_datetime():
22912292
np.datetime64("NaT"),
22922293
]
22932294
)
2294-
tm.assert_series_equal(res, expected)
2295+
tm.assert_series_equal(res, expected)

0 commit comments

Comments
 (0)