Skip to content

Commit 65f5c41

Browse files
committed
BUG: close #30050 - initial solution
1 parent f9fb02e commit 65f5c41

File tree

3 files changed

+35
-1
lines changed

3 files changed

+35
-1
lines changed

doc/source/whatsnew/v1.0.0.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -796,6 +796,7 @@ Datetimelike
796796
- Bug in :meth:`DataFrame.append` would remove the timezone-awareness of new data (:issue:`30238`)
797797
- Bug in :meth:`Series.cummin` and :meth:`Series.cummax` with timezone-aware dtype incorrectly dropping its timezone (:issue:`15553`)
798798
- Bug in :class:`DatetimeArray`, :class:`TimedeltaArray`, and :class:`PeriodArray` where inplace addition and subtraction did not actually operate inplace (:issue:`24115`)
799+
- Bug in :func:`pandas.to_datetime` when called with ``Series`` storing ``IntegerArray`` raising ``TypeError`` instead of returning ``Series`` (:issue:`30050`)
799800

800801
Timedelta
801802
^^^^^^^^^

pandas/core/tools/datetimes.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@
3838
)
3939
from pandas.core.dtypes.missing import notna
4040

41+
from pandas._typing import ArrayLike
42+
from pandas.arrays import IntegerArray
4143
from pandas.core import algorithms
4244
from pandas.core.algorithms import unique
4345

@@ -317,7 +319,20 @@ def _convert_listlike_datetimes(
317319
if format is not None:
318320
raise ValueError("cannot specify both format and unit")
319321
arg = getattr(arg, "values", arg)
320-
result, tz_parsed = tslib.array_with_unit_to_datetime(arg, unit, errors=errors)
322+
# GH 30050 pass an ndarray to tslib.array_with_unit_to_datetime
323+
# because it expects an ndarray argument
324+
if isinstance(arg, IntegerArray):
325+
arg_np = np.array(arg[np.logical_not(arg._mask)], dtype=type(arg[0]))
326+
result_np, tz_parsed = tslib.array_with_unit_to_datetime(
327+
arg_np, unit, errors=errors
328+
)
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
332+
else:
333+
result, tz_parsed = tslib.array_with_unit_to_datetime(
334+
arg, unit, errors=errors
335+
)
321336
if errors == "ignore":
322337
from pandas import Index
323338

pandas/tests/indexes/datetimes/test_tools.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2291,3 +2291,21 @@ def test_should_cache_errors(unique_share, check_count, err_message):
22912291

22922292
with pytest.raises(AssertionError, match=err_message):
22932293
tools.should_cache(arg, unique_share, check_count)
2294+
2295+
def test_intarray_to_datetime():
2296+
# Test for #30050
2297+
ser = pd.Series([1, 2, None, 2 ** 61, None])
2298+
ser = ser.astype("Int64")
2299+
2300+
res = pd.to_datetime(ser, unit="ns")
2301+
2302+
expected = pd.Series(
2303+
[
2304+
np.datetime64("1970-01-01 00:00:00.000000001"),
2305+
np.datetime64("1970-01-01 00:00:00.000000002"),
2306+
np.datetime64("NaT"),
2307+
np.datetime64("2043-01-25 23:56:49.213693952"),
2308+
np.datetime64("NaT"),
2309+
]
2310+
)
2311+
tm.assert_series_equal(res, expected)

0 commit comments

Comments
 (0)