Skip to content

Commit e0efa14

Browse files
TomAugspurgerPingviinituutti
authored andcommitted
Fixed itertuples usage in to_dict (pandas-dev#24965)
* Fixed itertuples usage in to_dict Closes pandas-dev#24940 Closes pandas-dev#24939
1 parent 0132f80 commit e0efa14

File tree

3 files changed

+30
-6
lines changed

3 files changed

+30
-6
lines changed

doc/source/whatsnew/v0.24.1.rst

+7
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,13 @@ Whats New in 0.24.1 (February XX, 2019)
1515
These are the changes in pandas 0.24.1. See :ref:`release` for a full changelog
1616
including other versions of pandas.
1717

18+
.. _whatsnew_0241.regressions:
19+
20+
Fixed Regressions
21+
^^^^^^^^^^^^^^^^^
22+
23+
- Bug in :meth:`DataFrame.itertuples` with ``records`` orient raising an ``AttributeError`` when the ``DataFrame`` contained more than 255 columns (:issue:`24939`)
24+
- Bug in :meth:`DataFrame.itertuples` orient converting integer column names to strings prepended with an underscore (:issue:`24940`)
1825

1926
.. _whatsnew_0241.enhancements:
2027

pandas/core/frame.py

+9-6
Original file line numberDiff line numberDiff line change
@@ -847,7 +847,7 @@ def itertuples(self, index=True, name="Pandas"):
847847
----------
848848
index : bool, default True
849849
If True, return the index as the first element of the tuple.
850-
name : str, default "Pandas"
850+
name : str or None, default "Pandas"
851851
The name of the returned namedtuples or None to return regular
852852
tuples.
853853
@@ -1290,23 +1290,26 @@ def to_dict(self, orient='dict', into=dict):
12901290
('columns', self.columns.tolist()),
12911291
('data', [
12921292
list(map(com.maybe_box_datetimelike, t))
1293-
for t in self.itertuples(index=False)]
1294-
)))
1293+
for t in self.itertuples(index=False, name=None)
1294+
])))
12951295
elif orient.lower().startswith('s'):
12961296
return into_c((k, com.maybe_box_datetimelike(v))
12971297
for k, v in compat.iteritems(self))
12981298
elif orient.lower().startswith('r'):
1299+
columns = self.columns.tolist()
1300+
rows = (dict(zip(columns, row))
1301+
for row in self.itertuples(index=False, name=None))
12991302
return [
13001303
into_c((k, com.maybe_box_datetimelike(v))
1301-
for k, v in compat.iteritems(row._asdict()))
1302-
for row in self.itertuples(index=False)]
1304+
for k, v in compat.iteritems(row))
1305+
for row in rows]
13031306
elif orient.lower().startswith('i'):
13041307
if not self.index.is_unique:
13051308
raise ValueError(
13061309
"DataFrame index must be unique for orient='index'."
13071310
)
13081311
return into_c((t[0], dict(zip(self.columns, t[1:])))
1309-
for t in self.itertuples())
1312+
for t in self.itertuples(name=None))
13101313
else:
13111314
raise ValueError("orient '{o}' not understood".format(o=orient))
13121315

pandas/tests/frame/test_convert_to.py

+14
Original file line numberDiff line numberDiff line change
@@ -488,3 +488,17 @@ def test_to_dict_index_dtypes(self, into, expected):
488488
result = DataFrame.from_dict(result, orient='index')[cols]
489489
expected = DataFrame.from_dict(expected, orient='index')[cols]
490490
tm.assert_frame_equal(result, expected)
491+
492+
def test_to_dict_numeric_names(self):
493+
# https://github.com/pandas-dev/pandas/issues/24940
494+
df = DataFrame({str(i): [i] for i in range(5)})
495+
result = set(df.to_dict('records')[0].keys())
496+
expected = set(df.columns)
497+
assert result == expected
498+
499+
def test_to_dict_wide(self):
500+
# https://github.com/pandas-dev/pandas/issues/24939
501+
df = DataFrame({('A_{:d}'.format(i)): [i] for i in range(256)})
502+
result = df.to_dict('records')[0]
503+
expected = {'A_{:d}'.format(i): i for i in range(256)}
504+
assert result == expected

0 commit comments

Comments
 (0)