-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
BUG: Index Name is not displayed with header=False in to_csv #24840
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -50,7 +50,18 @@ def __init__(self, obj, path_or_buf=None, sep=",", na_rep='', | |||||||||||||||||||
|
||||||||||||||||||||
self.header = header | ||||||||||||||||||||
self.index = index | ||||||||||||||||||||
self.index_label = index_label | ||||||||||||||||||||
|
||||||||||||||||||||
# if index label is not explicitly called, index label is True if | ||||||||||||||||||||
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. blank line here 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. added |
||||||||||||||||||||
# header or index is not False; otherwise, index label is set to False | ||||||||||||||||||||
if index_label is None: | ||||||||||||||||||||
if self.header is False or self.header is None or not self.index: | ||||||||||||||||||||
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.
Suggested change
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. yeah... thanks @WillAyd ... however, it doesn't seem equal in my test. Because by default, 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. So 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. can you update this
seems equivalent 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. it does seem equivalent though,
for instance, since from version 0.24.0, it looks like 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. try |
||||||||||||||||||||
self.index_label = False | ||||||||||||||||||||
else: | ||||||||||||||||||||
self.index_label = True | ||||||||||||||||||||
else: | ||||||||||||||||||||
# if index label is explicitly called, then use the caller. | ||||||||||||||||||||
self.index_label = index_label | ||||||||||||||||||||
|
||||||||||||||||||||
self.mode = mode | ||||||||||||||||||||
if encoding is None: | ||||||||||||||||||||
encoding = 'ascii' if compat.PY2 else 'utf-8' | ||||||||||||||||||||
|
@@ -188,6 +199,40 @@ def save(self): | |||||||||||||||||||
for _fh in handles: | ||||||||||||||||||||
_fh.close() | ||||||||||||||||||||
|
||||||||||||||||||||
def _index_label_encoder(self): | ||||||||||||||||||||
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. can you add a doc-string 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. there is pandas/pandas/io/formats/format.py Lines 840 to 848 in f57bd72
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. Potentially. Let's revisit once tests are passing again. 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. future PR though. |
||||||||||||||||||||
""" | ||||||||||||||||||||
Encode index label if it is not False. | ||||||||||||||||||||
|
||||||||||||||||||||
Returns | ||||||||||||||||||||
------- | ||||||||||||||||||||
index_label: list | ||||||||||||||||||||
New index_label given index types | ||||||||||||||||||||
encode_labels: list | ||||||||||||||||||||
List of index labels | ||||||||||||||||||||
""" | ||||||||||||||||||||
index_label = self.index_label | ||||||||||||||||||||
obj = self.obj | ||||||||||||||||||||
|
||||||||||||||||||||
if index_label is True: | ||||||||||||||||||||
index_label = [] | ||||||||||||||||||||
# append index label based on index type | ||||||||||||||||||||
if isinstance(obj.index, ABCMultiIndex): | ||||||||||||||||||||
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. can we not simply do 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. changed! |
||||||||||||||||||||
# add empty string is name is None | ||||||||||||||||||||
index_label = list(map(lambda name: name or '', | ||||||||||||||||||||
obj.index.names)) | ||||||||||||||||||||
else: | ||||||||||||||||||||
# if no name, use empty string | ||||||||||||||||||||
if obj.index.name is None: | ||||||||||||||||||||
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. i don't htink you need this branch at all 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. i think it's needed, and looks like if the branch is removed, lots of tests will fail 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. see above the cases for multiindex and index are the same. |
||||||||||||||||||||
index_label.append('') | ||||||||||||||||||||
else: | ||||||||||||||||||||
index_label.append(obj.index.name) | ||||||||||||||||||||
elif not isinstance(index_label, | ||||||||||||||||||||
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. When does the code go down this branch? 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. sorry, should be |
||||||||||||||||||||
(list, tuple, np.ndarray, ABCIndexClass)): | ||||||||||||||||||||
index_label = [index_label] | ||||||||||||||||||||
|
||||||||||||||||||||
encoded_labels = list(index_label) | ||||||||||||||||||||
return index_label, encoded_labels | ||||||||||||||||||||
|
||||||||||||||||||||
def _save_header(self): | ||||||||||||||||||||
|
||||||||||||||||||||
writer = self.writer | ||||||||||||||||||||
|
@@ -200,8 +245,16 @@ def _save_header(self): | |||||||||||||||||||
|
||||||||||||||||||||
has_aliases = isinstance(header, (tuple, list, np.ndarray, | ||||||||||||||||||||
ABCIndexClass)) | ||||||||||||||||||||
if not (has_aliases or self.header): | ||||||||||||||||||||
return | ||||||||||||||||||||
if not (has_aliases or header): | ||||||||||||||||||||
# if index_label is False, nothing will display. | ||||||||||||||||||||
if index_label is False: | ||||||||||||||||||||
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. Does it matter if this is None? 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. it depends, if index label is not explicitly called which is |
||||||||||||||||||||
return | ||||||||||||||||||||
else: | ||||||||||||||||||||
# based on index_label value, encoded labels are given | ||||||||||||||||||||
index_label, encoded_labels = self._index_label_encoder() | ||||||||||||||||||||
encoded_labels.extend([''] * len(obj.columns)) | ||||||||||||||||||||
writer.writerow(encoded_labels) | ||||||||||||||||||||
return | ||||||||||||||||||||
if has_aliases: | ||||||||||||||||||||
if len(header) != len(cols): | ||||||||||||||||||||
raise ValueError(('Writing {ncols} cols but got {nalias} ' | ||||||||||||||||||||
|
@@ -215,27 +268,16 @@ def _save_header(self): | |||||||||||||||||||
if self.index: | ||||||||||||||||||||
# should write something for index label | ||||||||||||||||||||
if index_label is not False: | ||||||||||||||||||||
if index_label is None: | ||||||||||||||||||||
if isinstance(obj.index, ABCMultiIndex): | ||||||||||||||||||||
index_label = [] | ||||||||||||||||||||
for i, name in enumerate(obj.index.names): | ||||||||||||||||||||
if name is None: | ||||||||||||||||||||
name = '' | ||||||||||||||||||||
index_label.append(name) | ||||||||||||||||||||
else: | ||||||||||||||||||||
index_label = obj.index.name | ||||||||||||||||||||
if index_label is None: | ||||||||||||||||||||
index_label = [''] | ||||||||||||||||||||
else: | ||||||||||||||||||||
index_label = [index_label] | ||||||||||||||||||||
elif not isinstance(index_label, | ||||||||||||||||||||
(list, tuple, np.ndarray, ABCIndexClass)): | ||||||||||||||||||||
# given a string for a DF with Index | ||||||||||||||||||||
index_label = [index_label] | ||||||||||||||||||||
|
||||||||||||||||||||
encoded_labels = list(index_label) | ||||||||||||||||||||
index_label, encoded_labels = self._index_label_encoder() | ||||||||||||||||||||
else: | ||||||||||||||||||||
encoded_labels = [] | ||||||||||||||||||||
# if index is multiindex, multiple empty labels are provided | ||||||||||||||||||||
if isinstance(obj.index, ABCMultiIndex): | ||||||||||||||||||||
index_label = [] | ||||||||||||||||||||
index_label.extend([''] * len(obj.index.names)) | ||||||||||||||||||||
# if index is single index, list of empty string is provided | ||||||||||||||||||||
else: | ||||||||||||||||||||
index_label = [''] | ||||||||||||||||||||
encoded_labels = list(index_label) | ||||||||||||||||||||
|
||||||||||||||||||||
if not has_mi_columns or has_aliases: | ||||||||||||||||||||
encoded_labels += list(write_cols) | ||||||||||||||||||||
|
Uh oh!
There was an error while loading. Please reload this page.