Skip to content

Commit 4b37274

Browse files
committed
ENH: Timedelta isoformat
1 parent 1f82f18 commit 4b37274

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

pandas/io/json.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -883,3 +883,29 @@ def _recursive_extract(data, path, seen_meta, level=0):
883883
result[k] = np.array(v).repeat(lengths)
884884

885885
return result
886+
887+
# ---------------------------------------------------------------------
888+
# JSON-Table Schema routines
889+
# http://specs.frictionlessdata.io/json-table-schema/
890+
891+
892+
# TODO: Make method on Timedelta?
893+
def isoformat(x):
894+
"""
895+
Format Timedelta as ISO 8601 Duration
896+
897+
Examples
898+
--------
899+
>>> td = pd.Timedelta(days=6, minutes=50, seconds=3,
900+
... milliseconds=10, microseconds=10, nanoseconds=12)
901+
>>> isoformat(td)
902+
'Pn6Tn0n50n3.010010012'
903+
"""
904+
components = x.components
905+
seconds = '{}.{:0>3}{:0>3}{:0>3}'.format(components.seconds,
906+
components.milliseconds,
907+
components.microseconds,
908+
components.nanoseconds)
909+
tpl = 'Pn{td.days}Tn{td.hours}n{td.minutes}n{seconds}'.format(
910+
td=components, seconds=seconds)
911+
return tpl

0 commit comments

Comments
 (0)