Skip to content

Commit

Permalink
Backport PR pandas-dev#24965: Fixed itertuples usage in to_dict
Browse files Browse the repository at this point in the history
  • Loading branch information
TomAugspurger authored and MeeseeksDev[bot] committed Jan 28, 2019
1 parent e22a6c8 commit bd39e73
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 6 deletions.
7 changes: 7 additions & 0 deletions doc/source/whatsnew/v0.24.1.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@ Whats New in 0.24.1 (February XX, 2019)
These are the changes in pandas 0.24.1. See :ref:`release` for a full changelog
including other versions of pandas.

.. _whatsnew_0241.regressions:

Fixed Regressions
^^^^^^^^^^^^^^^^^

- Bug in :meth:`DataFrame.itertuples` with ``records`` orient raising an ``AttributeError`` when the ``DataFrame`` contained more than 255 columns (:issue:`24939`)
- Bug in :meth:`DataFrame.itertuples` orient converting integer column names to strings prepended with an underscore (:issue:`24940`)

.. _whatsnew_0241.enhancements:

Expand Down
15 changes: 9 additions & 6 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -847,7 +847,7 @@ def itertuples(self, index=True, name="Pandas"):
----------
index : bool, default True
If True, return the index as the first element of the tuple.
name : str, default "Pandas"
name : str or None, default "Pandas"
The name of the returned namedtuples or None to return regular
tuples.
Expand Down Expand Up @@ -1290,23 +1290,26 @@ def to_dict(self, orient='dict', into=dict):
('columns', self.columns.tolist()),
('data', [
list(map(com.maybe_box_datetimelike, t))
for t in self.itertuples(index=False)]
)))
for t in self.itertuples(index=False, name=None)
])))
elif orient.lower().startswith('s'):
return into_c((k, com.maybe_box_datetimelike(v))
for k, v in compat.iteritems(self))
elif orient.lower().startswith('r'):
columns = self.columns.tolist()
rows = (dict(zip(columns, row))
for row in self.itertuples(index=False, name=None))
return [
into_c((k, com.maybe_box_datetimelike(v))
for k, v in compat.iteritems(row._asdict()))
for row in self.itertuples(index=False)]
for k, v in compat.iteritems(row))
for row in rows]
elif orient.lower().startswith('i'):
if not self.index.is_unique:
raise ValueError(
"DataFrame index must be unique for orient='index'."
)
return into_c((t[0], dict(zip(self.columns, t[1:])))
for t in self.itertuples())
for t in self.itertuples(name=None))
else:
raise ValueError("orient '{o}' not understood".format(o=orient))

Expand Down
14 changes: 14 additions & 0 deletions pandas/tests/frame/test_convert_to.py
Original file line number Diff line number Diff line change
Expand Up @@ -488,3 +488,17 @@ def test_to_dict_index_dtypes(self, into, expected):
result = DataFrame.from_dict(result, orient='index')[cols]
expected = DataFrame.from_dict(expected, orient='index')[cols]
tm.assert_frame_equal(result, expected)

def test_to_dict_numeric_names(self):
# https://github.com/pandas-dev/pandas/issues/24940
df = DataFrame({str(i): [i] for i in range(5)})
result = set(df.to_dict('records')[0].keys())
expected = set(df.columns)
assert result == expected

def test_to_dict_wide(self):
# https://github.com/pandas-dev/pandas/issues/24939
df = DataFrame({('A_{:d}'.format(i)): [i] for i in range(256)})
result = df.to_dict('records')[0]
expected = {'A_{:d}'.format(i): i for i in range(256)}
assert result == expected

0 comments on commit bd39e73

Please sign in to comment.