Skip to content

Commit 7be068b

Browse files
committed
ENH: Add Timedelta Support to JSON Reader with orient=table (#21140)
1 parent 0a0b2b9 commit 7be068b

File tree

2 files changed

+8
-9
lines changed

2 files changed

+8
-9
lines changed

pandas/io/json/table_schema.py

+5-6
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import warnings
77

88
import pandas._libs.json as json
9-
from pandas import DataFrame
9+
from pandas import DataFrame, Timedelta
1010
from pandas.api.types import CategoricalDtype
1111
import pandas.core.common as com
1212
from pandas.core.dtypes.common import (
@@ -163,7 +163,7 @@ def convert_json_field_to_pandas_type(field):
163163
elif typ == 'boolean':
164164
return 'bool'
165165
elif typ == 'duration':
166-
return 'timedelta64'
166+
return 'timedelta64[ns]'
167167
elif typ == 'datetime':
168168
if field.get('tz'):
169169
return 'datetime64[ns, {tz}]'.format(tz=field['tz'])
@@ -306,10 +306,9 @@ def parse_table_schema(json, precise_float):
306306
raise NotImplementedError('table="orient" can not yet read timezone '
307307
'data')
308308

309-
# No ISO constructor for Timedelta as of yet, so need to raise
310-
if 'timedelta64' in dtypes.values():
311-
raise NotImplementedError('table="orient" can not yet read '
312-
'ISO-formatted Timedelta data')
309+
for col, dtype in dtypes.items():
310+
if dtype == 'timedelta64[ns]':
311+
df[col] = df[col].apply(Timedelta)
313312

314313
df = df.astype(dtypes)
315314

pandas/tests/io/json/test_json_table_schema.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,7 @@ def test_convert_pandas_type_to_json_field_categorical(self, kind,
390390
({'type': 'integer'}, 'int64'),
391391
({'type': 'number'}, 'float64'),
392392
({'type': 'boolean'}, 'bool'),
393-
({'type': 'duration'}, 'timedelta64'),
393+
({'type': 'duration'}, 'timedelta64[ns]'),
394394
({'type': 'datetime'}, 'datetime64[ns]'),
395395
({'type': 'datetime', 'tz': 'US/Hawaii'}, 'datetime64[ns, US/Hawaii]'),
396396
({'type': 'any'}, 'object'),
@@ -499,6 +499,7 @@ class TestTableOrientReader(object):
499499
'level_0'])
500500
@pytest.mark.parametrize("vals", [
501501
{'ints': [1, 2, 3, 4]},
502+
{'timedeltas': pd.timedelta_range('1H', periods=4, freq='T')},
502503
{'objects': ['a', 'b', 'c', 'd']},
503504
{'date_ranges': pd.date_range('2016-01-01', freq='d', periods=4)},
504505
{'categoricals': pd.Series(pd.Categorical(['a', 'b', 'c', 'c']))},
@@ -516,7 +517,6 @@ def test_read_json_table_orient(self, index_nm, vals, recwarn):
516517
@pytest.mark.parametrize("index_nm", [
517518
None, "idx", "index"])
518519
@pytest.mark.parametrize("vals", [
519-
{'timedeltas': pd.timedelta_range('1H', periods=4, freq='T')},
520520
{'timezones': pd.date_range('2016-01-01', freq='d', periods=4,
521521
tz='US/Central')}])
522522
def test_read_json_table_orient_raises(self, index_nm, vals, recwarn):
@@ -530,7 +530,7 @@ def test_comprehensive(self):
530530
{'A': [1, 2, 3, 4],
531531
'B': ['a', 'b', 'c', 'c'],
532532
'C': pd.date_range('2016-01-01', freq='d', periods=4),
533-
# 'D': pd.timedelta_range('1H', periods=4, freq='T'),
533+
'D': pd.timedelta_range('1H', periods=4, freq='T'),
534534
'E': pd.Series(pd.Categorical(['a', 'b', 'c', 'c'])),
535535
'F': pd.Series(pd.Categorical(['a', 'b', 'c', 'c'],
536536
ordered=True)),

0 commit comments

Comments
 (0)