Skip to content

Commit c5ebd19

Browse files
Aloqeelypmhatre1
authored andcommitted
DEPR: 'epoch' date format in to_json (pandas-dev#57987)
1 parent 5f00812 commit c5ebd19

File tree

6 files changed

+184
-36
lines changed

6 files changed

+184
-36
lines changed

doc/source/user_guide/io.rst

+2-9
Original file line numberDiff line numberDiff line change
@@ -1949,13 +1949,6 @@ Writing in ISO date format, with microseconds:
19491949
json = dfd.to_json(date_format="iso", date_unit="us")
19501950
json
19511951
1952-
Epoch timestamps, in seconds:
1953-
1954-
.. ipython:: python
1955-
1956-
json = dfd.to_json(date_format="epoch", date_unit="s")
1957-
json
1958-
19591952
Writing to a file, with a date index and a date column:
19601953

19611954
.. ipython:: python
@@ -1965,7 +1958,7 @@ Writing to a file, with a date index and a date column:
19651958
dfj2["ints"] = list(range(5))
19661959
dfj2["bools"] = True
19671960
dfj2.index = pd.date_range("20130101", periods=5)
1968-
dfj2.to_json("test.json")
1961+
dfj2.to_json("test.json", date_format="iso")
19691962
19701963
with open("test.json") as fh:
19711964
print(fh.read())
@@ -2140,7 +2133,7 @@ Dates written in nanoseconds need to be read back in nanoseconds:
21402133
.. ipython:: python
21412134
21422135
from io import StringIO
2143-
json = dfj2.to_json(date_unit="ns")
2136+
json = dfj2.to_json(date_format="iso", date_unit="ns")
21442137
21452138
# Try to parse timestamps as milliseconds -> Won't Work
21462139
dfju = pd.read_json(StringIO(json), date_unit="ms")

doc/source/whatsnew/v3.0.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,7 @@ Other Deprecations
196196
- Deprecated allowing non-keyword arguments in :meth:`DataFrame.all`, :meth:`DataFrame.min`, :meth:`DataFrame.max`, :meth:`DataFrame.sum`, :meth:`DataFrame.prod`, :meth:`DataFrame.mean`, :meth:`DataFrame.median`, :meth:`DataFrame.sem`, :meth:`DataFrame.var`, :meth:`DataFrame.std`, :meth:`DataFrame.skew`, :meth:`DataFrame.kurt`, :meth:`Series.all`, :meth:`Series.min`, :meth:`Series.max`, :meth:`Series.sum`, :meth:`Series.prod`, :meth:`Series.mean`, :meth:`Series.median`, :meth:`Series.sem`, :meth:`Series.var`, :meth:`Series.std`, :meth:`Series.skew`, and :meth:`Series.kurt`. (:issue:`57087`)
197197
- Deprecated allowing non-keyword arguments in :meth:`Series.to_markdown` except ``buf``. (:issue:`57280`)
198198
- Deprecated allowing non-keyword arguments in :meth:`Series.to_string` except ``buf``. (:issue:`57280`)
199+
- Deprecated using ``epoch`` date format in :meth:`DataFrame.to_json` and :meth:`Series.to_json`, use ``iso`` instead. (:issue:`57063`)
199200
-
200201

201202
.. ---------------------------------------------------------------------------

pandas/core/generic.py

+21
Original file line numberDiff line numberDiff line change
@@ -2338,6 +2338,11 @@ def to_json(
23382338
'iso' = ISO8601. The default depends on the `orient`. For
23392339
``orient='table'``, the default is 'iso'. For all other orients,
23402340
the default is 'epoch'.
2341+
2342+
.. deprecated:: 3.0.0
2343+
'epoch' date format is deprecated and will be removed in a future
2344+
version, please use 'iso' instead.
2345+
23412346
double_precision : int, default 10
23422347
The number of decimal places to use when encoding
23432348
floating point values. The possible maximal value is 15.
@@ -2540,6 +2545,22 @@ def to_json(
25402545
date_format = "iso"
25412546
elif date_format is None:
25422547
date_format = "epoch"
2548+
dtypes = self.dtypes if self.ndim == 2 else [self.dtype]
2549+
if any(lib.is_np_dtype(dtype, "mM") for dtype in dtypes):
2550+
warnings.warn(
2551+
"The default 'epoch' date format is deprecated and will be removed "
2552+
"in a future version, please use 'iso' date format instead.",
2553+
FutureWarning,
2554+
stacklevel=find_stack_level(),
2555+
)
2556+
elif date_format == "epoch":
2557+
# GH#57063
2558+
warnings.warn(
2559+
"'epoch' date format is deprecated and will be removed in a future "
2560+
"version, please use 'iso' date format instead.",
2561+
FutureWarning,
2562+
stacklevel=find_stack_level(),
2563+
)
25432564

25442565
config.is_nonnegative_int(indent)
25452566
indent = indent or 0

pandas/tests/io/json/test_json_table_schema.py

+8-3
Original file line numberDiff line numberDiff line change
@@ -451,12 +451,17 @@ def test_to_json_categorical_index(self):
451451
assert result == expected
452452

453453
def test_date_format_raises(self, df_table):
454-
msg = (
454+
error_msg = (
455455
"Trying to write with `orient='table'` and `date_format='epoch'`. Table "
456456
"Schema requires dates to be formatted with `date_format='iso'`"
457457
)
458-
with pytest.raises(ValueError, match=msg):
459-
df_table.to_json(orient="table", date_format="epoch")
458+
warning_msg = (
459+
"'epoch' date format is deprecated and will be removed in a future "
460+
"version, please use 'iso' date format instead."
461+
)
462+
with pytest.raises(ValueError, match=error_msg):
463+
with tm.assert_produces_warning(FutureWarning, match=warning_msg):
464+
df_table.to_json(orient="table", date_format="epoch")
460465

461466
# others work
462467
df_table.to_json(orient="table", date_format="iso")

0 commit comments

Comments
 (0)