|
18 | 18 | is_integer_dtype,
|
19 | 19 | is_datetime_or_timedelta_dtype,
|
20 | 20 | is_bool_dtype, is_scalar,
|
21 |
| - _string_dtypes, |
| 21 | + is_string_dtype, _string_dtypes, |
22 | 22 | pandas_dtype,
|
23 | 23 | _ensure_int8, _ensure_int16,
|
24 | 24 | _ensure_int32, _ensure_int64,
|
@@ -965,12 +965,16 @@ def maybe_cast_to_datetime(value, dtype, errors='raise'):
|
965 | 965 | if is_datetime64:
|
966 | 966 | value = to_datetime(value, errors=errors)._values
|
967 | 967 | elif is_datetime64tz:
|
968 |
| - # input has to be UTC at this point, so just |
969 |
| - # localize |
970 |
| - value = (to_datetime(value, errors=errors) |
971 |
| - .tz_localize('UTC') |
972 |
| - .tz_convert(dtype.tz) |
973 |
| - ) |
| 968 | + is_dt_string = is_string_dtype(value) |
| 969 | + value = to_datetime(value, errors=errors) |
| 970 | + if is_dt_string: |
| 971 | + # Strings here are naive, so directly localize |
| 972 | + value = value.tz_localize(dtype.tz) |
| 973 | + else: |
| 974 | + # Numeric values are UTC at this point, |
| 975 | + # so localize and convert |
| 976 | + value = (value.tz_localize('UTC') |
| 977 | + .tz_convert(dtype.tz)) |
974 | 978 | elif is_timedelta64:
|
975 | 979 | value = to_timedelta(value, errors=errors)._values
|
976 | 980 | except (AttributeError, ValueError, TypeError):
|
|
0 commit comments