Skip to content

[DOCS] Add example of how to preserve order of columns with usecols. #19746

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 3 commits into from
Feb 21, 2018
Merged

[DOCS] Add example of how to preserve order of columns with usecols. #19746

merged 3 commits into from
Feb 21, 2018

Conversation

EricChea
Copy link
Contributor

What's this PR do?

Adding an example of how to preserve order when using the usecols parameter. I've noticed that some folks are still unaware that the columns will not necessarily be returned in the specified order passed to usecols.

@codecov
Copy link

codecov bot commented Feb 18, 2018

Codecov Report

Merging #19746 into master will increase coverage by 0.02%.
The diff coverage is n/a.

Impacted file tree graph

@@            Coverage Diff             @@
##           master   #19746      +/-   ##
==========================================
+ Coverage   91.58%    91.6%   +0.02%     
==========================================
  Files         150      150              
  Lines       48862    48864       +2     
==========================================
+ Hits        44750    44763      +13     
+ Misses       4112     4101      -11
Flag Coverage Δ
#multiple 89.98% <ø> (+0.02%) ⬆️
#single 41.75% <ø> (ø) ⬆️
Impacted Files Coverage Δ
pandas/io/parsers.py 95.32% <ø> (ø) ⬆️
pandas/core/ops.py 96.45% <0%> (-0.39%) ⬇️
pandas/core/indexes/base.py 96.45% <0%> (-0.01%) ⬇️
pandas/core/arrays/categorical.py 94.9% <0%> (+0.01%) ⬆️
pandas/core/indexes/category.py 97.31% <0%> (+0.03%) ⬆️
pandas/core/frame.py 97.22% <0%> (+0.06%) ⬆️
pandas/plotting/_converter.py 66.95% <0%> (+1.73%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 192bd46...68efc4b. Read the comment docs.

Element order is ignored, so ``usecols=[0, 1]`` is the same as ``[1, 0]``.
Element order is ignored, so ``usecols=[0, 1]`` is the same as ``[1, 0]``. Element
order is ignored, so usecols=[1,0] is the same as [0,1]. To instantiate a
DataFrame with element order preserved use ``pd.read_csv(usecols=[0, 1])[[0, 1]]``
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it should be “pd.read_csv(usecols=[0, 1])[[1, 0]]”

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep.

Copy link
Contributor Author

@EricChea EricChea Feb 18, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for catching that. Fixed in commit f137bd0

@TomAugspurger TomAugspurger added this to the 0.23.0 milestone Feb 18, 2018
@@ -136,7 +136,11 @@ usecols : array-like or callable, default ``None``
that correspond to column names provided either by the user in `names` or
inferred from the document header row(s). For example, a valid array-like
`usecols` parameter would be ``[0, 1, 2]`` or ``['foo', 'bar', 'baz']``.
Element order is ignored, so ``usecols=[0, 1]`` is the same as ``[1, 0]``.
Element order is ignored, so ``usecols=[0, 1]`` is the same as ``[1, 0]``. Element
order is ignored, so usecols=[1,0] is the same as [0,1]. To instantiate a
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you put these usecols=[] in double back ticks as well? And commas between the elements.

Finally, I have a slight preference for using names like ['foo', 'bar'] instead of [0, 1], even though it's a bit longer.

Copy link
Contributor Author

@EricChea EricChea Feb 18, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. Please refer to f137bd0

I went ahead and encased usecols in double back ticks.

Updated column names per your suggestion. I was originally trying to keep consistent with the line before Element order is ignored, so usecols=[0, 1] is the same as [1, 0].

@pep8speaks
Copy link

pep8speaks commented Feb 18, 2018

Hello @EricChea! Thanks for updating the PR.

Cheers ! There are no PEP8 issues in this Pull Request. 🍻

Comment last updated on February 18, 2018 at 18:27 Hours UTC

Copy link
Contributor

@jreback jreback left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

small change otherwise lgtm.

@@ -136,7 +136,12 @@ usecols : array-like or callable, default ``None``
that correspond to column names provided either by the user in `names` or
inferred from the document header row(s). For example, a valid array-like
`usecols` parameter would be ``[0, 1, 2]`` or ``['foo', 'bar', 'baz']``.
Element order is ignored, so ``usecols=[0, 1]`` is the same as ``[1, 0]``.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you add a blank line before the additions.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure thing. Please refer to 68efc4b

@gfyoung
Copy link
Member

gfyoung commented Feb 21, 2018

ping @jreback @jorisvandenbossche : this is green and ready to merge

@TomAugspurger TomAugspurger merged commit cd1b168 into pandas-dev:master Feb 21, 2018
@TomAugspurger
Copy link
Contributor

Thanks @EricChea!

@EricChea EricChea deleted the mod_docs branch February 22, 2018 05:16
@EricChea
Copy link
Contributor Author

Thanks for reviewing @TomAugspurger

harisbal pushed a commit to harisbal/pandas that referenced this pull request Feb 28, 2018
…andas-dev#19746)

* Add example of how to preserve order of columns with usecols.

* Encase usecols in double back ticks for consistency. Change column names from numeric to string.

* Add line to separate examples.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants