|
20 | 20 | is_integer_dtype,
|
21 | 21 | is_datetime_or_timedelta_dtype,
|
22 | 22 | is_bool_dtype, is_scalar,
|
23 |
| - _string_dtypes, |
| 23 | + is_string_dtype, _string_dtypes, |
24 | 24 | pandas_dtype,
|
25 | 25 | _ensure_int8, _ensure_int16,
|
26 | 26 | _ensure_int32, _ensure_int64,
|
@@ -1003,12 +1003,18 @@ def maybe_cast_to_datetime(value, dtype, errors='raise'):
|
1003 | 1003 | if is_datetime64:
|
1004 | 1004 | value = to_datetime(value, errors=errors)._values
|
1005 | 1005 | elif is_datetime64tz:
|
1006 |
| - # input has to be UTC at this point, so just |
1007 |
| - # localize |
1008 |
| - value = (to_datetime(value, errors=errors) |
1009 |
| - .tz_localize('UTC') |
1010 |
| - .tz_convert(dtype.tz) |
1011 |
| - ) |
| 1006 | + # This block can be simplified once PR #17413 is |
| 1007 | + # complete |
| 1008 | + is_dt_string = is_string_dtype(value) |
| 1009 | + value = to_datetime(value, errors=errors) |
| 1010 | + if is_dt_string: |
| 1011 | + # Strings here are naive, so directly localize |
| 1012 | + value = value.tz_localize(dtype.tz) |
| 1013 | + else: |
| 1014 | + # Numeric values are UTC at this point, |
| 1015 | + # so localize and convert |
| 1016 | + value = (value.tz_localize('UTC') |
| 1017 | + .tz_convert(dtype.tz)) |
1012 | 1018 | elif is_timedelta64:
|
1013 | 1019 | value = to_timedelta(value, errors=errors)._values
|
1014 | 1020 | except (AttributeError, ValueError, TypeError):
|
|
0 commit comments