-
-
Notifications
You must be signed in to change notification settings - Fork 18.6k
DOC: update the pandas.errors.DtypeWarning docstring #20208
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
jorisvandenbossche
merged 8 commits into
pandas-dev:master
from
hissashirocha:docstring_dtype_warning
Mar 13, 2018
Merged
Changes from 6 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
6cbdd7d
Improving docstring for DtypeWarning.
hissashirocha ab7f790
Improving docstring for DtypeWarning.
hissashirocha 9423867
Improving docstring for DtypeWarning.
hissashirocha ed7e372
Improving docstring for DtypeWarning.
hissashirocha 76fc248
Improving docstring for DtypeWarning.
hissashirocha 12c0ac5
Merge remote-tracking branch 'upstream/master' into docstring_dtype_w…
hissashirocha 9be8cfd
Updated example
TomAugspurger 9e7e129
move os import
jorisvandenbossche File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -38,9 +38,53 @@ class ParserError(ValueError): | |
|
||
class DtypeWarning(Warning): | ||
""" | ||
Warning that is raised for a dtype incompatibility. This | ||
can happen whenever `pd.read_csv` encounters non- | ||
uniform dtypes in a column(s) of a given CSV file. | ||
Warning raised when reading different dtypes in a column from a file. | ||
|
||
Raised for a dtype incompatibility. This can happen whenever `read_csv` | ||
or `read_table` encounter non-uniform dtypes in a column(s) of a given | ||
CSV file. | ||
|
||
See Also | ||
-------- | ||
pandas.read_csv : Read CSV (comma-separated) file into a DataFrame. | ||
pandas.read_table : Read general delimited file into a DataFrame. | ||
|
||
Notes | ||
----- | ||
This warning is issued when dealing with larger files because the dtype | ||
checking happens per chunk read. | ||
|
||
Despite the warning, the CSV file is read with mixed types in a single | ||
column which will be an object type. See the examples below to better | ||
understand this issue. | ||
|
||
Examples | ||
-------- | ||
This example creates and reads a large CSV file with a column that contains | ||
`int` and `str`. | ||
|
||
>>> df = pd.DataFrame({'a':['1']*100000 + ['X']*100000 + ['1']*100000, | ||
... 'b':['b']*300000}) | ||
>>> df.to_csv('test.csv', index=False) | ||
>>> df2 = pd.read_csv('test.csv') | ||
>>> DtypeWarning: Columns (0) have mixed types... # doctest: +SKIP | ||
|
||
Important to notice that df2 will contain both `str` and `int` for the | ||
same input, '1'. | ||
|
||
>>> df2.iloc[262140, 0] | ||
'1' | ||
>>> type(df2.iloc[262140, 0]) | ||
<class 'str'> | ||
>>> df2.iloc[262150, 0] | ||
1 | ||
>>> type(df2.iloc[262150, 0]) | ||
<class 'int'> | ||
|
||
One way to solve this issue is using the parameter `converters` in the | ||
`read_csv` and `read_table` functions to explicit the conversion: | ||
|
||
>>> df2 = pd.read_csv('test', sep='\t', converters={'a': str}) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. FYI, this is still There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Another thing: I think we should recommend |
||
""" | ||
|
||
|
||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you remove the
>>>
on this line? (it might be you need to move the "# doctest: +SKIP" to the line above after the read_csv)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jorisvandenbossche @TomAugspurger I've added "import os" and an "os.remove('test.csv')" at the end of the example, just as you've agreed on #20302 , and also put "# doctest: +SKIP" right before the warning, like this:
But I got this message on validation:
################################################################################
################################### Doctests ###################################
################################################################################
Line 32, in pandas.errors.DtypeWarning
Failed example:
os.remove('test.csv')
Expected:
# doctest: +SKIP
DtypeWarning: Columns (0) have mixed types...
Got nothing
And also, removing ".csv" from to_csv and read_csv, like Tom suggested, raises two possible errors:
What do you guys recommend?