-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
DOC: Added note to io.rst regarding reading in mixed dtypes #13782
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
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
335d043
DOC: Added note to io.rst regarding reading in mixed dtypes
wcwagner b6e2b64
DOC: Swtiched Counter to value_counts, added low_memory alternative e…
wcwagner ba4c2ce
DOC: Added short commentary on alternatives
wcwagner 8112ad5
DOC: Shortened note, moved alternatives to main text
wcwagner 7400607
DOC: Added refs to basics.dtypes and basics.object_conversion, added …
wcwagner 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
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 |
---|---|---|
|
@@ -435,11 +435,71 @@ individual columns: | |
df = pd.read_csv(StringIO(data), dtype={'b': object, 'c': np.float64}) | ||
df.dtypes | ||
|
||
Fortunately, ``pandas`` offers more than one way to ensure that your column(s) | ||
contain only one ``dtype``. If you're unfamiliar with these concepts, you can | ||
see :ref:`here<basics.dtypes>` to learn more about dtypes, and | ||
:ref:`here<basics.object_conversion>` to learn more about ``object`` conversion in | ||
``pandas``. | ||
|
||
|
||
For instance, you can use the ``converters`` argument | ||
of :func:`~pandas.read_csv`: | ||
|
||
.. ipython:: python | ||
|
||
data = "col_1\n1\n2\n'A'\n4.22" | ||
df = pd.read_csv(StringIO(data), converters={'col_1':str}) | ||
df | ||
df['col_1'].apply(type).value_counts() | ||
|
||
Or you can use the :func:`~pandas.to_numeric` function to coerce the | ||
dtypes after reading in the data, | ||
|
||
.. ipython:: python | ||
|
||
df2 = pd.read_csv(StringIO(data)) | ||
df2['col_1'] = pd.to_numeric(df2['col_1'], errors='coerce') | ||
df2 | ||
df2['col_1'].apply(type).value_counts() | ||
|
||
which would convert all valid parsing to floats, leaving the invalid parsing | ||
as ``NaN``. | ||
|
||
Ultimately, how you deal with reading in columns containing mixed dtypes | ||
depends on your specific needs. In the case above, if you wanted to ``NaN`` out | ||
the data anomalies, then :func:`~pandas.to_numeric` is probably your best option. | ||
However, if you wanted for all the data to be coerced, no matter the type, then | ||
using the ``converters`` argument of :func:`~pandas.read_csv` would certainly be | ||
worth trying. | ||
|
||
.. note:: | ||
The ``dtype`` option is currently only supported by the C engine. | ||
Specifying ``dtype`` with ``engine`` other than 'c' raises a | ||
``ValueError``. | ||
|
||
.. note:: | ||
In some cases, reading in abnormal data with columns containing mixed dtypes | ||
will result in an inconsistent dataset. If you rely on pandas to infer the | ||
dtypes of your columns, the parsing engine will go and infer the dtypes for | ||
different chunks of the data, rather than the whole dataset at once. Consequently, | ||
you can end up with column(s) with mixed dtypes. For example, | ||
|
||
.. ipython:: python | ||
:okwarning: | ||
|
||
df = pd.DataFrame({'col_1':range(500000) + ['a', 'b'] + range(500000)}) | ||
df.to_csv('foo') | ||
mixed_df = pd.read_csv('foo') | ||
mixed_df['col_1'].apply(type).value_counts() | ||
mixed_df['col_1'].dtype | ||
|
||
will result with `mixed_df` containing an ``int`` dtype for certain chunks | ||
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.
explain what you actually mean here. There wasn't a problem, but the data instead had mixed dtypes. |
||
of the column, and ``str`` for others due to the mixed dtypes from the | ||
data that was read in. It is important to note that the overall column will be | ||
marked with a ``dtype`` of ``object``, which is used for columns with mixed dtypes. | ||
|
||
|
||
|
||
Naming and Using Columns | ||
'''''''''''''''''''''''' | ||
|
||
|
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
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.
add in here a reference to
basics.dtypes
. learn more about dtypes here.