-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
ENH: Improve OverflowError
message when pandas.read_stata
fails to convert dates
#36096
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
Comments
cc @bashtage if you have thoughts. |
The flag would be OK but it should only be part of Stata Reader, read stata. Read stata api is already quite crowded and adding a rarely used feature would just make things worse. Would it skip entire columns or just observations? If entire columns, would the columns raw values be returned? If single. Observations presumably these would be converted to NAT. Also, what produced this dta file? Is the date really out of range or does Stata not comply with its own specification (I've seen this once before, so not impossible). Finally, another option would be to |
The other option, possibly better would be to return object columns with datetime, when it data is out of range. |
👍
Preference to returning
The file name indicates it comes from Mexican National Health and Nutrition Surveys (ENSANUT).
I've opened the file with both Stata 13 and Stata 15. In both cases Stata displays the data as a numeric value in spite of the fact it carries a date format. Using Stata I dropped most rows and all but one column to create the file I uploaded and I received no warnings or errors. |
Does Stata display the other dates in the variable correctly, or is the entire column numeric? |
The minimum value is well into the future - they all display as numeric. |
Could you check what happens if you have some valid dates and some large values? Might help decide what return would have the highest fidelity with Stata. |
This suggests a path where out-of-range are left unmodified and in-range are returned as object. Thinking about the api, it could be a 3-way argument: |
How about a fourth argument value that returns regular/slow |
Do even out-of-range are converted? the hard part is knowing whether this is correct since Stata seems to refuse to convert these values. |
Testing indicates STATA will not do date operations on these columns e.g.,
For the case I want to investigate the data |
Well, you can always read the data without converting the dates. What is unclear to me is what sense does it make to convert dates that are outside of the stata spec? |
OverflowError
message when pandas.read_stata
fails to convert datesOverflowError
message when pandas.read_stata
fails to convert dates
Is your feature request related to a problem?
Calling
pd.read_stata
on a DTA file with a bad date format code leads to a minimally useful error message. The error is raised from this line.What I see:
Describe the solution you'd like
I'd like a clearer message indicating the problem column. It would be easy to catch the
OverflowError
in the loop doing the date conversion and include the column name.Desired detailed error:
OverflowError: Failed converting column tiempo_gen for format %dD_m_Y: date value out of range
Alternative error:
OverflowError: date value out of range for column tiempo_gen
date_overflow.DTA.gz
I would be happy to submit a patch for this if the solution is approved.
API breaking implications
I don't believe this would break or alter any APIs
Describe alternatives you've considered
Add a flag
skip_invalid_dates
which changes behavior to skip converting dates IFF they would error. Instead of raising an exception it would be caught and a warning emitted along the lines of"date value out of range for column tiempo_gen - skipping date conversion"
. This would allow users access to as much converted data as possible. This flag would default toFalse
and be backwards compatible with the APIs.Refactor the
pandas.io.stata
API to allow for manual date conversion without using private functions.Additional context
Output of
pd.show_versions()
INSTALLED VERSIONS
commit : 76f74d5
python : 3.7.8.final.0
python-bits : 64
OS : Linux
OS-release : 4.15.0-112-generic
Version : #113-Ubuntu SMP Thu Jul 9 23:41:39 UTC 2020
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8
pandas : 1.2.0.dev0+230.g76f74d53b.dirty
numpy : 1.19.1
pytz : 2020.1
dateutil : 2.8.1
pip : 20.1.1
setuptools : 49.2.0
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : None
IPython : None
pandas_datareader: None
bs4 : None
bottleneck : None
fsspec : None
fastparquet : None
gcsfs : None
matplotlib : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pytables : None
pyxlsb : None
s3fs : None
scipy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
numba : None
This was the test I put together to hammer out all the details so I knew what was happening with the file
tl;dr
Example file - a subset of a real life file I ran into this issue with: date_overflow.DTA.gz
The text was updated successfully, but these errors were encountered: