From e94e3b979fec65808dd9c41f704e1c58bb5fbb46 Mon Sep 17 00:00:00 2001 From: Brock Date: Thu, 4 Nov 2021 14:27:33 -0700 Subject: [PATCH 1/3] BUG: PeriodIndex in pytables --- pandas/io/pytables.py | 3 +++ pandas/tests/io/pytables/test_put.py | 4 +--- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/pandas/io/pytables.py b/pandas/io/pytables.py index f85128ea0ca4a..17448dbf76bb2 100644 --- a/pandas/io/pytables.py +++ b/pandas/io/pytables.py @@ -2074,6 +2074,9 @@ def convert(self, values: np.ndarray, nan_rep, encoding: str, errors: str): factory: type[Index] | type[DatetimeIndex] = Index if is_datetime64_dtype(values.dtype) or is_datetime64tz_dtype(values.dtype): factory = DatetimeIndex + elif values.dtype == "i8" and "freq" in kwargs: + # PeriodIndex data is stored as i8 + factory = lambda x, **kwds: PeriodIndex(ordinal=x, **kwds) # making an Index instance could throw a number of different errors try: diff --git a/pandas/tests/io/pytables/test_put.py b/pandas/tests/io/pytables/test_put.py index 3707079d03d64..f4b70bc6f238a 100644 --- a/pandas/tests/io/pytables/test_put.py +++ b/pandas/tests/io/pytables/test_put.py @@ -243,10 +243,8 @@ def check(format, index): check("table", index) check("fixed", index) - # period index currently broken for table - # seee GH7796 FIXME check("fixed", tm.makePeriodIndex) - # check('table',tm.makePeriodIndex) + check("table", tm.makePeriodIndex) # GH#7796 # unicode index = tm.makeUnicodeIndex From 6e6eba65734eb2cc943a3e4d6abeda98e16fcf8a Mon Sep 17 00:00:00 2001 From: Brock Date: Thu, 4 Nov 2021 21:38:56 -0700 Subject: [PATCH 2/3] mypy fixup --- pandas/io/pytables.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/io/pytables.py b/pandas/io/pytables.py index 17448dbf76bb2..f52e704ccc68f 100644 --- a/pandas/io/pytables.py +++ b/pandas/io/pytables.py @@ -2071,7 +2071,7 @@ def convert(self, values: np.ndarray, nan_rep, encoding: str, errors: str): if self.freq is not None: kwargs["freq"] = _ensure_decoded(self.freq) - factory: type[Index] | type[DatetimeIndex] = Index + factory: type[Index] | type[DatetimeIndex] | type[PeriodIndex] = Index if is_datetime64_dtype(values.dtype) or is_datetime64tz_dtype(values.dtype): factory = DatetimeIndex elif values.dtype == "i8" and "freq" in kwargs: From 9ad340fe1fd31388b5d3cb8804fa40e3ca1fc4e4 Mon Sep 17 00:00:00 2001 From: Brock Date: Fri, 5 Nov 2021 09:08:22 -0700 Subject: [PATCH 3/3] mypy fixup --- pandas/io/pytables.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/pandas/io/pytables.py b/pandas/io/pytables.py index f52e704ccc68f..8c8e9b9feeb80 100644 --- a/pandas/io/pytables.py +++ b/pandas/io/pytables.py @@ -2071,12 +2071,17 @@ def convert(self, values: np.ndarray, nan_rep, encoding: str, errors: str): if self.freq is not None: kwargs["freq"] = _ensure_decoded(self.freq) - factory: type[Index] | type[DatetimeIndex] | type[PeriodIndex] = Index + factory: type[Index] | type[DatetimeIndex] = Index if is_datetime64_dtype(values.dtype) or is_datetime64tz_dtype(values.dtype): factory = DatetimeIndex elif values.dtype == "i8" and "freq" in kwargs: # PeriodIndex data is stored as i8 - factory = lambda x, **kwds: PeriodIndex(ordinal=x, **kwds) + # error: Incompatible types in assignment (expression has type + # "Callable[[Any, KwArg(Any)], PeriodIndex]", variable has type + # "Union[Type[Index], Type[DatetimeIndex]]") + factory = lambda x, **kwds: PeriodIndex( # type: ignore[assignment] + ordinal=x, **kwds + ) # making an Index instance could throw a number of different errors try: