Problem description
This is a feature request for usecols to accept integer value and string. If integer/string is provided, flag squeeze
is to be turned on, thus producing a Series.
Rationale
- consistency with
index_col
which accepts both integer/string and lists - shortcut
Expected Output
# test
testfile = "pandas/tests/io/data/tips.csv"
df = pd.read_table(testfile, sep=",", usecols="tip")
assert isinstance(df, pd.Series)
df = pd.read_table(testfile, sep=",", usecols=2)
assert isinstance(df, pd.Series)
df = pd.read_table(testfile, sep=",", usecols=[2])
assert isinstance(df, pd.DataFrame)
Output of pd.show_versions()
Comment From: jreback
cc @gfyoung thoughts?
Comment From: gfyoung
I am open to accepting scalars for usecols
because that is indeed consistent with index_col
.
However, I am not a fan of squeezing to Series
automatically if a scalar is passed in. We shouldn't be coupling together squeeze
and usecols
behavior. The flags should be as independent of one another as possible. While I understand the rationale, from a design standpoint, I would be against this.
Comment From: jorisvandenbossche
I agree with @gfyoung on the squeeze. I also understand this would be nice, but it is not that it makes new behaviour possible, it just for saving a few keystrokes (squeeze=True
). In that light, I don't find adding more complexity to the usecols
keyword worth this (and squeeze=True
is also more explicit).
Regarding accepting a scalar, I have less an opinion about that. Probably not much harm, but I also don't really see the need (the current docs are very clear: either array-like or callable, which is more clear than scalar, array-like or callable)
Comment From: jreback
I will agree about automagically returning a Series. To be very honest, I find the squeeze
not really necessary either, and just additional code bloat. Reading a csv should always return a DataFrame, but that's a different discussion.
As far as usecols
accepting a scalar; a bit indifferent about that as well. Sure if if someone wants to do it, I think we would accept it.