|
23 | 23 | from pandas.core.dtypes.common import (
|
24 | 24 | _INT64_DTYPE,
|
25 | 25 | _NS_DTYPE,
|
| 26 | + is_bool_dtype, |
26 | 27 | is_categorical_dtype,
|
27 | 28 | is_datetime64_any_dtype,
|
28 | 29 | is_datetime64_dtype,
|
@@ -1903,32 +1904,36 @@ def maybe_convert_dtype(data, copy):
|
1903 | 1904 | ------
|
1904 | 1905 | TypeError : PeriodDType data is passed
|
1905 | 1906 | """
|
1906 |
| - if is_float_dtype(data): |
| 1907 | + if not hasattr(data, "dtype"): |
| 1908 | + # e.g. collections.deque |
| 1909 | + return data, copy |
| 1910 | + |
| 1911 | + if is_float_dtype(data.dtype): |
1907 | 1912 | # Note: we must cast to datetime64[ns] here in order to treat these
|
1908 | 1913 | # as wall-times instead of UTC timestamps.
|
1909 | 1914 | data = data.astype(_NS_DTYPE)
|
1910 | 1915 | copy = False
|
1911 | 1916 | # TODO: deprecate this behavior to instead treat symmetrically
|
1912 | 1917 | # with integer dtypes. See discussion in GH#23675
|
1913 | 1918 |
|
1914 |
| - elif is_timedelta64_dtype(data): |
| 1919 | + elif is_timedelta64_dtype(data.dtype) or is_bool_dtype(data.dtype): |
1915 | 1920 | # GH#29794 enforcing deprecation introduced in GH#23539
|
1916 | 1921 | raise TypeError(f"dtype {data.dtype} cannot be converted to datetime64[ns]")
|
1917 |
| - elif is_period_dtype(data): |
| 1922 | + elif is_period_dtype(data.dtype): |
1918 | 1923 | # Note: without explicitly raising here, PeriodIndex
|
1919 | 1924 | # test_setops.test_join_does_not_recur fails
|
1920 | 1925 | raise TypeError(
|
1921 | 1926 | "Passing PeriodDtype data is invalid. Use `data.to_timestamp()` instead"
|
1922 | 1927 | )
|
1923 | 1928 |
|
1924 |
| - elif is_categorical_dtype(data): |
| 1929 | + elif is_categorical_dtype(data.dtype): |
1925 | 1930 | # GH#18664 preserve tz in going DTI->Categorical->DTI
|
1926 | 1931 | # TODO: cases where we need to do another pass through this func,
|
1927 | 1932 | # e.g. the categories are timedelta64s
|
1928 | 1933 | data = data.categories.take(data.codes, fill_value=NaT)._values
|
1929 | 1934 | copy = False
|
1930 | 1935 |
|
1931 |
| - elif is_extension_array_dtype(data) and not is_datetime64tz_dtype(data): |
| 1936 | + elif is_extension_array_dtype(data.dtype) and not is_datetime64tz_dtype(data.dtype): |
1932 | 1937 | # Includes categorical
|
1933 | 1938 | # TODO: We have no tests for these
|
1934 | 1939 | data = np.array(data, dtype=np.object_)
|
|
0 commit comments