Skip to content

Commit 6399982

Browse files
authored
BUG: time strings cast to ArrowDtype with pa.time64 type (#56490)
1 parent f1fae79 commit 6399982

File tree

3 files changed

+28
-2
lines changed

3 files changed

+28
-2
lines changed

doc/source/whatsnew/v2.2.0.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -697,7 +697,7 @@ Other
697697
- Bug in rendering ``inf`` values inside a a :class:`DataFrame` with the ``use_inf_as_na`` option enabled (:issue:`55483`)
698698
- Bug in rendering a :class:`Series` with a :class:`MultiIndex` when one of the index level's names is 0 not having that name displayed (:issue:`55415`)
699699
- Bug in the error message when assigning an empty dataframe to a column (:issue:`55956`)
700-
-
700+
- Bug when time-like strings were being cast to :class:`ArrowDtype` with ``pyarrow.time64`` type (:issue:`56463`)
701701

702702
.. ***DO NOT USE THIS SECTION***
703703

pandas/core/arrays/arrow/array.py

+17-1
Original file line numberDiff line numberDiff line change
@@ -493,7 +493,23 @@ def _box_pa_array(
493493
if pa.types.is_dictionary(pa_type):
494494
pa_array = pa_array.dictionary_encode()
495495
else:
496-
pa_array = pa_array.cast(pa_type)
496+
try:
497+
pa_array = pa_array.cast(pa_type)
498+
except (
499+
pa.ArrowInvalid,
500+
pa.ArrowTypeError,
501+
pa.ArrowNotImplementedError,
502+
):
503+
if pa.types.is_string(pa_array.type) or pa.types.is_large_string(
504+
pa_array.type
505+
):
506+
# TODO: Move logic in _from_sequence_of_strings into
507+
# _box_pa_array
508+
return cls._from_sequence_of_strings(
509+
value, dtype=pa_type
510+
)._pa_array
511+
else:
512+
raise
497513

498514
return pa_array
499515

pandas/tests/extension/test_arrow.py

+10
Original file line numberDiff line numberDiff line change
@@ -3051,3 +3051,13 @@ def test_string_to_datetime_parsing_cast():
30513051
ArrowExtensionArray(pa.array(pd.to_datetime(string_dates), from_pandas=True))
30523052
)
30533053
tm.assert_series_equal(result, expected)
3054+
3055+
3056+
def test_string_to_time_parsing_cast():
3057+
# GH 56463
3058+
string_times = ["11:41:43.076160"]
3059+
result = pd.Series(string_times, dtype="time64[us][pyarrow]")
3060+
expected = pd.Series(
3061+
ArrowExtensionArray(pa.array([time(11, 41, 43, 76160)], from_pandas=True))
3062+
)
3063+
tm.assert_series_equal(result, expected)

0 commit comments

Comments
 (0)