a generified fixed version
| 2971 | |
| 2972 | |
| 2973 | class GenericFixed(Fixed): |
| 2974 | """a generified fixed version""" |
| 2975 | |
| 2976 | _index_type_map = {DatetimeIndex: "datetime", PeriodIndex: "period"} |
| 2977 | _reverse_index_map = {v: k for k, v in _index_type_map.items()} |
| 2978 | attributes: list[str] = [] |
| 2979 | |
| 2980 | # indexer helpers |
| 2981 | def _class_to_alias(self, cls) -> str: |
| 2982 | return self._index_type_map.get(cls, "") |
| 2983 | |
| 2984 | def _alias_to_class(self, alias): |
| 2985 | if isinstance(alias, type): # pragma: no cover |
| 2986 | # compat: for a short period of time master stored types |
| 2987 | return alias |
| 2988 | return self._reverse_index_map.get(alias, Index) |
| 2989 | |
| 2990 | def _get_index_factory(self, attrs): |
| 2991 | index_class = self._alias_to_class(getattr(attrs, "index_class", "")) |
| 2992 | |
| 2993 | factory: Callable |
| 2994 | |
| 2995 | kwargs = {} |
| 2996 | if index_class == DatetimeIndex: |
| 2997 | |
| 2998 | def f(values, freq=None, tz=None): |
| 2999 | # data are already in UTC, localize and convert if tz present |
| 3000 | dta = DatetimeArray._simple_new( |
| 3001 | values.values, dtype=values.dtype, freq=freq |
| 3002 | ) |
| 3003 | result = DatetimeIndex._simple_new(dta, name=None) |
| 3004 | if tz is not None: |
| 3005 | result = result.tz_localize("UTC").tz_convert(tz) |
| 3006 | return result |
| 3007 | |
| 3008 | factory = f |
| 3009 | elif index_class == PeriodIndex: |
| 3010 | |
| 3011 | def f(values, freq=None, tz=None): |
| 3012 | dtype = PeriodDtype(freq) |
| 3013 | parr = PeriodArray._simple_new(values, dtype=dtype) |
| 3014 | return PeriodIndex._simple_new(parr, name=None) |
| 3015 | |
| 3016 | factory = f |
| 3017 | else: |
| 3018 | factory = index_class |
| 3019 | kwargs["copy"] = False |
| 3020 | |
| 3021 | if "freq" in attrs: |
| 3022 | kwargs["freq"] = attrs["freq"] |
| 3023 | if index_class is Index: |
| 3024 | # DTI/PI would be gotten by _alias_to_class |
| 3025 | factory = TimedeltaIndex |
| 3026 | |
| 3027 | if "tz" in attrs: |
| 3028 | kwargs["tz"] = attrs["tz"] |
| 3029 | assert index_class is DatetimeIndex # just checking |
| 3030 |