|
64 | 64 | ) |
65 | 65 | from pandas.core.dtypes.missing import isna |
66 | 66 |
|
| 67 | +from pandas import Timedelta |
67 | 68 | from pandas.core.arrays import datetimelike as dtl |
68 | 69 | from pandas.core.arrays._ranges import generate_regular_range |
69 | 70 | import pandas.core.common as com |
|
93 | 94 |
|
94 | 95 | from pandas import ( |
95 | 96 | DataFrame, |
96 | | - Timedelta, |
97 | 97 | ) |
98 | 98 | from pandas.core.arrays import PeriodArray |
99 | 99 |
|
@@ -824,27 +824,27 @@ def _add_offset(self, offset: BaseOffset) -> Self: |
824 | 824 | "s", |
825 | 825 | ] |
826 | 826 | res_unit = self.unit |
827 | | - # Only try to adjust unit if both units are recognized |
828 | | - try: |
829 | | - if hasattr(offset, "offset"): |
830 | | - offset_td = Timedelta(offset.offset) |
831 | | - offset_unit = offset_td.unit |
832 | | - if self.unit in units and offset_unit in units: |
833 | | - idx_self = units.index(self.unit) |
834 | | - idx_offset = units.index(offset_unit) |
835 | | - res_unit = units[min(idx_self, idx_offset)] |
836 | | - except Exception: |
837 | | - res_unit = self.unit |
838 | | - dtype = tz_to_dtype(self.tz, unit=res_unit) |
839 | | - if res_values.dtype != f"datetime64[{res_unit}]": |
840 | | - res_values = res_values.astype(f"datetime64[{res_unit}]") |
841 | | - result = type(self)._simple_new(res_values, dtype=dtype) |
| 827 | + if hasattr(offset, "offset"): |
| 828 | + offset_td = Timedelta(offset.offset) |
| 829 | + offset_unit = offset_td.unit |
| 830 | + if self.unit in units and offset_unit in units: |
| 831 | + idx_self = units.index(self.unit) |
| 832 | + idx_offset = units.index(offset_unit) |
| 833 | + res_unit = units[min(idx_self, idx_offset)] |
| 834 | + dtype_naive = np.dtype(f"datetime64[{res_unit}]") |
| 835 | + if res_values.dtype != dtype_naive: |
| 836 | + res_values = res_values.astype(dtype_naive) |
| 837 | + result = type(self)._simple_new(res_values, dtype=dtype_naive) |
842 | 838 |
|
843 | 839 | if offset.normalize: |
844 | 840 | result = result.normalize() |
845 | 841 | result._freq = None |
846 | 842 |
|
847 | | - if self.tz is not None: |
| 843 | + if ( |
| 844 | + self.tz is not None |
| 845 | + and getattr(result.dtype, "tz", None) is None |
| 846 | + and res_unit == "ns" |
| 847 | + ): |
848 | 848 | result = result.tz_localize(self.tz) |
849 | 849 |
|
850 | 850 | return result |
|
0 commit comments