Patch-Source: https://github.com/jsonpickle/jsonpickle/commit/a24240bfdec6a9d5172c2f25e19654d23ffc61e1 -- From a24240bfdec6a9d5172c2f25e19654d23ffc61e1 Mon Sep 17 00:00:00 2001 From: Theelx <43764914+Theelx@users.noreply.github.com> Date: Fri, 26 May 2023 14:05:05 -0400 Subject: [PATCH] Implement compatibility with pandas 2 --- jsonpickle/ext/pandas.py | 7 ++++++- requirements-dev.txt | 2 +- setup.cfg | 2 +- tests/pandas_test.py | 2 +- 4 files changed, 9 insertions(+), 4 deletions(-) diff --git a/jsonpickle/ext/pandas.py b/jsonpickle/ext/pandas.py index af7538b..94d8cd7 100644 --- a/jsonpickle/ext/pandas.py +++ b/jsonpickle/ext/pandas.py @@ -62,10 +62,13 @@ def make_read_csv_params(meta, context): parse_dates = [] converters = {} timedeltas = [] + # this is only for pandas v2+ due to a backwards-incompatible change + parse_datetime_v2 = {} dtype = {} for k, v in meta_dtypes.items(): if v.startswith('datetime'): parse_dates.append(k) + parse_datetime_v2[k] = v elif v.startswith('complex'): converters[k] = complex elif v.startswith('timedelta'): @@ -79,6 +82,7 @@ def make_read_csv_params(meta, context): dtype=dtype, header=header, parse_dates=parse_dates, converters=converters ), timedeltas, + parse_datetime_v2, ) @@ -104,7 +108,7 @@ class PandasDfHandler(BaseHandler): def restore(self, data): csv, meta = self.pp.restore_pandas(data) - params, timedeltas = make_read_csv_params(meta, self.context) + params, timedeltas, parse_datetime_v2 = make_read_csv_params(meta, self.context) # None makes it compatible with objects serialized before # column_levels_names has been introduced. column_level_names = meta.get('column_level_names', None) @@ -115,6 +119,7 @@ class PandasDfHandler(BaseHandler): ) for col in timedeltas: df[col] = pd.to_timedelta(df[col]) + df = df.astype(parse_datetime_v2) df.set_index(decode(meta['index']), inplace=True) # restore the column level(s) name(s) diff --git a/tests/pandas_test.py b/tests/pandas_test.py index 403ead2..530449f 100644 --- a/tests/pandas_test.py +++ b/tests/pandas_test.py @@ -62,7 +62,7 @@ def test_dataframe_roundtrip(): 'an_inf': np.array([np.inf] * 3), 'a_str': np.str_('foo'), 'a_unicode': np.unicode_('bar'), - 'date': np.array([np.datetime64('2014-01-01')] * 3), + 'date': np.array([np.datetime64('2014-01-01')] * 3, dtype="datetime64[s]"), 'complex': np.complex_([1 - 2j, 2 - 1.2j, 3 - 1.3j]), # TODO: the following dtypes are not currently supported. # 'object': np.object_([{'a': 'b'}]*3),