Skip to content

Commit

Permalink
Fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris Bertinato committed Jan 18, 2019
1 parent 20be993 commit 2f81938
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 48 deletions.
14 changes: 2 additions & 12 deletions pandas/io/parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -3141,28 +3141,18 @@ def _get_lines(self, rows=None):
def _make_date_converter(date_parser=None, dayfirst=False,
infer_datetime_format=False):
def converter(*date_cols):
from pandas.core.dtypes.common import is_datetime64_dtype
if date_parser is None:
strs = _concat_date_cols(date_cols)

try:
dti = tools.to_datetime(
return tools.to_datetime(
ensure_object(strs),
utc=None,
dayfirst=dayfirst,
errors='ignore',
infer_datetime_format=infer_datetime_format
)

if isinstance(dti, DatetimeIndex):
dti = dti.to_pydatetime()
# elif is_datetime64_dtype(dti):
# dti = np.array([Timestamp(ts).to_pydatetime()
# for ts in dti])
else:
dti = dti.to_numpy()
).to_numpy()

return dti
except ValueError:
return tools.to_datetime(
parsing.try_parse_dates(strs, dayfirst=dayfirst))
Expand Down
68 changes: 32 additions & 36 deletions pandas/tests/indexes/datetimes/test_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -358,47 +358,43 @@ def test_to_datetime_dt64s(self, cache):
def test_to_datetime_array_of_dt64s(self, cache):
dts = [np.datetime64('2000-01-01'), np.datetime64('2000-01-02'), ]

with tm.assert_produces_warning(FutureWarning):

# Assuming all datetimes are in bounds, to_datetime() returns
# an array that is equal to Timestamp() parsing
tm.assert_numpy_array_equal(
pd.to_datetime(dts, box=False, cache=cache),
np.array([Timestamp(x).asm8 for x in dts])
)
# Assuming all datetimes are in bounds, to_datetime() returns
# an array that is equal to Timestamp() parsing
tm.assert_numpy_array_equal(
pd.to_datetime(dts, cache=cache).to_numpy(),
np.array([Timestamp(x).asm8 for x in dts])
)

# A list of datetimes where the last one is out of bounds
dts_with_oob = dts + [np.datetime64('9999-01-01')]
# A list of datetimes where the last one is out of bounds
dts_with_oob = dts + [np.datetime64('9999-01-01')]

pytest.raises(ValueError, pd.to_datetime, dts_with_oob,
errors='raise')
with pytest.raises(ValueError):
pd.to_datetime(dts_with_oob)

with tm.assert_produces_warning(FutureWarning):
tm.assert_numpy_array_equal(
pd.to_datetime(dts_with_oob, box=False, errors='coerce',
cache=cache),
np.array(
[
Timestamp(dts_with_oob[0]).asm8,
Timestamp(dts_with_oob[1]).asm8,
tslib.iNaT,
],
dtype='M8'
)
tm.assert_numpy_array_equal(
pd.to_datetime(dts_with_oob, errors='coerce',
cache=cache).to_numpy(),
np.array(
[
Timestamp(dts_with_oob[0]).asm8,
Timestamp(dts_with_oob[1]).asm8,
tslib.iNaT,
],
dtype='M8'
)

with tm.assert_produces_warning(FutureWarning):
# With errors='ignore', out of bounds datetime64s
# are converted to their .item(), which depending on the version of
# numpy is either a python datetime.datetime or datetime.date
tm.assert_numpy_array_equal(
pd.to_datetime(dts_with_oob, box=False, errors='ignore',
cache=cache),
np.array(
[dt.item() for dt in dts_with_oob],
dtype='O'
)
)

# With errors='ignore', out of bounds datetime64s
# are converted to their .item(), which depending on the version of
# numpy is either a python datetime.datetime or datetime.date
tm.assert_numpy_array_equal(
pd.to_datetime(dts_with_oob, errors='ignore',
cache=cache).to_numpy(),
np.array(
[dt.item() for dt in dts_with_oob],
dtype='O'
)
)

@pytest.mark.parametrize('cache', [True, False])
def test_to_datetime_tz(self, cache):
Expand Down

0 comments on commit 2f81938

Please sign in to comment.