Skip to content

BUG: Bug in to_datetime when format='%Y%m%d and coerce=True are specified (GH7930) #7931

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 4, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion doc/source/v0.15.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,8 @@ Bug Fixes
- Bug in ``Series.astype("unicode")`` not calling ``unicode`` on the values correctly (:issue:`7758`)
- Bug in ``DataFrame.as_matrix()`` with mixed ``datetime64[ns]`` and ``timedelta64[ns]`` dtypes (:issue:`7778`)
- Bug in ``HDFStore.select_column()`` not preserving UTC timezone info when selecting a DatetimeIndex (:issue:`7777`)

- Bug in ``to_datetime`` when ``format='%Y%m%d'`` and ``coerce=True`` are specified, where previously an object array was returned (rather than
a coerced time-series with ``NaT``), (:issue:`7930`)
- Bug in ``DatetimeIndex`` and ``PeriodIndex`` in-place addition and subtraction cause different result from normal one (:issue:`6527`)
- Bug in adding and subtracting ``PeriodIndex`` with ``PeriodIndex`` raise ``TypeError`` (:issue:`7741`)
- Bug in ``combine_first`` with ``PeriodIndex`` data raises ``TypeError`` (:issue:`3367`)
Expand Down
10 changes: 10 additions & 0 deletions pandas/tseries/tests/test_timeseries.py
Original file line number Diff line number Diff line change
Expand Up @@ -3934,6 +3934,16 @@ def test_to_datetime_format_YYYYMMDD(self):
result = to_datetime(s,format='%Y%m%d')
assert_series_equal(result, expected)

# coercion
# GH 7930
s = Series([20121231, 20141231, 99991231])
result = pd.to_datetime(s,format='%Y%m%d')
expected = np.array([ datetime(2012,12,31), datetime(2014,12,31), datetime(9999,12,31) ], dtype=object)
self.assert_numpy_array_equal(result, expected)

result = pd.to_datetime(s,format='%Y%m%d', coerce=True)
expected = Series(['20121231','20141231','NaT'],dtype='M8[ns]')
assert_series_equal(result, expected)

def test_to_datetime_format_microsecond(self):
val = '01-Apr-2011 00:00:01.978'
Expand Down
6 changes: 3 additions & 3 deletions pandas/tseries/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ def _convert_listlike(arg, box, format):
# shortcut formatting here
if format == '%Y%m%d':
try:
result = _attempt_YYYYMMDD(arg)
result = _attempt_YYYYMMDD(arg, coerce=coerce)
except:
raise ValueError("cannot convert the input to '%Y%m%d' date format")

Expand Down Expand Up @@ -313,14 +313,14 @@ def _convert_listlike(arg, box, format):
class DateParseError(ValueError):
pass

def _attempt_YYYYMMDD(arg):
def _attempt_YYYYMMDD(arg, coerce):
""" try to parse the YYYYMMDD/%Y%m%d format, try to deal with NaT-like,
arg is a passed in as an object dtype, but could really be ints/strings with nan-like/or floats (e.g. with nan) """

def calc(carg):
# calculate the actual result
carg = carg.astype(object)
return lib.try_parse_year_month_day(carg/10000,carg/100 % 100, carg % 100)
return tslib.array_to_datetime(lib.try_parse_year_month_day(carg/10000,carg/100 % 100, carg % 100), coerce=coerce)

def calc_with_mask(carg,mask):
result = np.empty(carg.shape, dtype='M8[ns]')
Expand Down