Skip to content

DOC/BUG: pd.to_datetime example produces different results from documentation #14448

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

Closed
AnkurDedania opened this issue Oct 18, 2016 · 4 comments
Labels
Datetime Datetime data dtype Docs
Milestone

Comments

@AnkurDedania
Copy link

A small, complete example of the issue

referring too http://pandas.pydata.org/pandas-docs/stable/generated/pandas.to_datetime.html

The example provides different results.

# Your code here
import pandas as pd
pd.to_datetime('13000101', format='%Y%m%d')

raises...

Traceback (most recent call last):
  File "C:\Miniconda3\lib\site-packages\pandas\tseries\tools.py", line 404, in _convert_listlike
    values, tz = tslib.datetime_to_datetime64(arg)
  File "pandas\tslib.pyx", line 1754, in pandas.tslib.datetime_to_datetime64 (pandas\tslib.c:30557)
TypeError: Unrecognized value type: <class 'str'>

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Miniconda3\lib\site-packages\pandas\util\decorators.py", line 91, in wrapper
    return func(*args, **kwargs)
  File "C:\Miniconda3\lib\site-packages\pandas\tseries\tools.py", line 424, in to_datetime
    return _convert_listlike(np.array([arg]), box, format)[0]
  File "C:\Miniconda3\lib\site-packages\pandas\tseries\tools.py", line 407, in _convert_listlike
    raise e
  File "C:\Miniconda3\lib\site-packages\pandas\tseries\tools.py", line 374, in _convert_listlike
    errors=errors)
  File "pandas\tslib.pyx", line 3721, in pandas.tslib.array_strptime (pandas\tslib.c:60980)
  File "pandas\tslib.pyx", line 3716, in pandas.tslib.array_strptime (pandas\tslib.c:60878)
  File "pandas\tslib.pyx", line 1718, in pandas.tslib._check_dts_bounds (pandas\tslib.c:30034)
pandas.tslib.OutOfBoundsDatetime: Out of bounds nanosecond timestamp: 1300-01-01 00:00:00

Expected Output

(according to example in docs)

datetime.datetime(1300, 1, 1, 0, 0)

Output of pd.show_versions()

pd.show_versions()

INSTALLED VERSIONS

commit: None
python: 3.5.2.final.0
python-bits: 64
OS: Windows
OS-release: 7
machine: AMD64
processor: Intel64 Family 6 Model 63 Stepping 2, GenuineIntel
byteorder: little
LC_ALL: None
LANG: None
LOCALE: None.None

pandas: 0.19.0
nose: 1.3.7
pip: 8.1.2
setuptools: 28.0.0
Cython: 0.24.1
numpy: 1.11.2
scipy: 0.18.1
statsmodels: 0.6.1
xarray: 0.8.2
IPython: 5.1.0
sphinx: 1.4.8
patsy: 0.4.1
dateutil: 2.5.3
pytz: 2016.7
blosc: 1.4.1
bottleneck: 1.1.0
tables: None
numexpr: 2.6.0
matplotlib: 1.5.3
openpyxl: 2.4.0
xlrd: 1.0.0
xlwt: 1.1.2
xlsxwriter: 0.9.3
lxml: 3.6.4
bs4: 4.5.0
html5lib: None
httplib2: None
apiclient: None
sqlalchemy: 1.1.0
pymysql: 0.7.9.None
psycopg2: None
jinja2: 2.8
boto: None
pandas_datareader: None

@AnkurDedania AnkurDedania changed the title DOC/BUG: example provides different information DOC/BUG: pd.to_datetime example produces different results from documentation Oct 18, 2016
@chris-b1
Copy link
Contributor

I'm guessing this was a side effect of #10674. I'm actually in favor of the new behavior - makes it much more obvious there is a problem, but I suppose this behavior might have been useful in some cases.

@chris-b1 chris-b1 added Datetime Datetime data dtype API Design Needs Discussion Requires discussion from core team before further action labels Oct 19, 2016
@jreback
Copy link
Contributor

jreback commented Oct 19, 2016

the doc-string needs to be updated to use ignore

In [2]: import pandas as pd
   ...: pd.to_datetime('13000101', format='%Y%m%d',errors='ignore')
Out[2]: datetime.datetime(1300, 1, 1, 0, 0)

@jreback jreback added Docs Difficulty Novice and removed API Design Needs Discussion Requires discussion from core team before further action labels Oct 19, 2016
@jreback jreback added this to the Next Major Release milestone Oct 19, 2016
@chris-b1
Copy link
Contributor

@jreback - oh, I see, I had tried without the format, which I guess never returned a plain datetime, so agree it's just docs

In [26]: pd.to_datetime('13000101', errors='ignore')
Out[26]: '13000101'

@jorisvandenbossche
Copy link
Member

It is a bit inconsistent that the return type changes on whether format is specified or not:

In [1]: pd.to_datetime('13000101', errors='ignore')
Out[1]: '13000101'

In [2]: pd.to_datetime('13000101', errors='ignore', format='%Y%m%d')
Out[2]: datetime.datetime(1300, 1, 1, 0, 0)

@jreback jreback modified the milestones: 0.19.1, Next Major Release Oct 20, 2016
jorisvandenbossche pushed a commit to jorisvandenbossche/pandas that referenced this issue Nov 2, 2016
…for out-of-bounds timestamps

with errors='ignore'

closes pandas-dev#14448

Author: Keshav Ramaswamy <[email protected]>
Author: Keshav Ramaswamy <[email protected]>

Closes pandas-dev#14452 from keshavramaswamy/master and squashes the following commits:

5468fc5 [Keshav Ramaswamy] added link to timestamp limitations in docstring
3aa78cf [Keshav Ramaswamy] edit docstring to fit python standards
8bfa58e [Keshav Ramaswamy] edited docstring to fit python standards
5ed8ef5 [Keshav Ramaswamy] edut docstring to fit Python Standards
7402de4 [Keshav Ramaswamy] edited docstring to fit python standards
c16ad6b [Keshav Ramaswamy] added timestamp limitations and default behavior to docstring
0db07b5 [Keshav Ramaswamy] updated docstring in .to_datetime()

(cherry picked from commit 18fba53)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Datetime Datetime data dtype Docs
Projects
None yet
4 participants