Skip to content

Commit c06ce81

Browse files
committed
Merge pull request #10876 from kawochen/BUG-FIX-10747-2
BUG: GH10747 where 'timestamp' is not inferred to be datetime column
2 parents 54f02df + 20d75d0 commit c06ce81

File tree

3 files changed

+38
-10
lines changed

3 files changed

+38
-10
lines changed

doc/source/io.rst

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1484,7 +1484,13 @@ be set to ``False`` if you need to preserve string-like numbers (e.g. '1', '2')
14841484

14851485
.. note::
14861486

1487-
Large integer values may be converted to dates if ``convert_dates=True`` and the data and / or column labels appear 'date-like'. The exact threshold depends on the ``date_unit`` specified.
1487+
Large integer values may be converted to dates if ``convert_dates=True`` and the data and / or column labels appear 'date-like'. The exact threshold depends on the ``date_unit`` specified. 'date-like' means that the column label meets one of the following criteria:
1488+
1489+
* it ends with ``'_at'``
1490+
* it ends with ``'_time'``
1491+
* it begins with ``'timestamp'``
1492+
* it is ``'modified'``
1493+
* it is ``'date'``
14881494

14891495
.. warning::
14901496

pandas/io/json.py

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,18 @@ def read_json(path_or_buf=None, orient=None, typ='frame', dtype=True,
150150
Try to convert the axes to the proper dtypes.
151151
convert_dates : boolean, default True
152152
List of columns to parse for dates; If True, then try to parse
153-
datelike columns default is True
153+
datelike columns default is True; a column label is datelike if
154+
155+
* it ends with ``'_at'``,
156+
157+
* it ends with ``'_time'``,
158+
159+
* it begins with ``'timestamp'``,
160+
161+
* it is ``'modified'``, or
162+
163+
* it is ``'date'``
164+
154165
keep_default_dates : boolean, default True.
155166
If parsing dates, then parse the default datelike columns
156167
numpy : boolean, default False
@@ -543,11 +554,13 @@ def is_ok(col):
543554
if not isinstance(col, compat.string_types):
544555
return False
545556

546-
if (col.endswith('_at') or
547-
col.endswith('_time') or
548-
col.lower() == 'modified' or
549-
col.lower() == 'date' or
550-
col.lower() == 'datetime'):
557+
col_lower = col.lower()
558+
if (col_lower.endswith('_at') or
559+
col_lower.endswith('_time') or
560+
col_lower == 'modified' or
561+
col_lower == 'date' or
562+
col_lower == 'datetime' or
563+
col_lower.startswith('timestamp')):
551564
return True
552565
return False
553566

pandas/io/tests/test_json/test_pandas.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
# pylint: disable-msg=W0612,E1101
22
from pandas.compat import range, lrange, StringIO, OrderedDict
3-
from pandas import compat
43
import os
54

65
import numpy as np
7-
from pandas import Series, DataFrame, DatetimeIndex, Timestamp, CategoricalIndex
6+
from pandas import (Series, DataFrame, DatetimeIndex, Timestamp, CategoricalIndex,
7+
read_json, compat)
88
from datetime import timedelta
99
import pandas as pd
10-
read_json = pd.read_json
1110

1211
from pandas.util.testing import (assert_almost_equal, assert_frame_equal,
1312
assert_series_equal, network,
@@ -574,6 +573,16 @@ def test_convert_dates(self):
574573
result = read_json(json, typ='series')
575574
assert_series_equal(result, ts)
576575

576+
def test_convert_dates_infer(self):
577+
#GH10747
578+
infer_words = ['trade_time', 'date', 'datetime', 'sold_at',
579+
'modified', 'timestamp', 'timestamps']
580+
for infer_word in infer_words:
581+
data = [{'id': 1, infer_word: 1036713600000}, {'id': 2}]
582+
expected = DataFrame([[1, Timestamp('2002-11-08')], [2, pd.NaT]], columns=['id', infer_word])
583+
result = read_json(pd.json.dumps(data))[['id', infer_word]]
584+
assert_frame_equal(result, expected)
585+
577586
def test_date_format_frame(self):
578587
df = self.tsframe.copy()
579588

0 commit comments

Comments
 (0)