|
5 | 5 | import pytest
|
6 | 6 | import pytz
|
7 | 7 | import collections
|
| 8 | +from collections import OrderedDict, defaultdict |
8 | 9 | import numpy as np
|
9 | 10 |
|
10 | 11 | from pandas import compat
|
@@ -288,3 +289,29 @@ def test_frame_to_dict_tz(self):
|
288 | 289 | ]
|
289 | 290 | tm.assert_dict_equal(result[0], expected[0])
|
290 | 291 | tm.assert_dict_equal(result[1], expected[1])
|
| 292 | + |
| 293 | + @pytest.mark.parametrize('into, expected', [ |
| 294 | + (dict, {0: {'int_col': 1, 'float_col': 1.0}, |
| 295 | + 1: {'int_col': 2, 'float_col': 2.0}, |
| 296 | + 2: {'int_col': 3, 'float_col': 3.0}}), |
| 297 | + (OrderedDict, OrderedDict([(0, {'int_col': 1, 'float_col': 1.0}), |
| 298 | + (1, {'int_col': 2, 'float_col': 2.0}), |
| 299 | + (2, {'int_col': 3, 'float_col': 3.0})])), |
| 300 | + (defaultdict(list), defaultdict(list, |
| 301 | + {0: {'int_col': 1, 'float_col': 1.0}, |
| 302 | + 1: {'int_col': 2, 'float_col': 2.0}, |
| 303 | + 2: {'int_col': 3, 'float_col': 3.0}})) |
| 304 | + ]) |
| 305 | + def test_to_dict_index_dtypes(self, into, expected): |
| 306 | + # GH 18580 |
| 307 | + # When using to_dict(orient='index') on a dataframe with int |
| 308 | + # and float columns only the int columns were cast to float |
| 309 | + |
| 310 | + df = DataFrame({'int_col': [1, 2, 3], |
| 311 | + 'float_col': [1.0, 2.0, 3.0]}) |
| 312 | + |
| 313 | + result = df.to_dict(orient='index', into=into) |
| 314 | + cols = ['int_col', 'float_col'] |
| 315 | + result = DataFrame.from_dict(result, orient='index')[cols] |
| 316 | + expected = DataFrame.from_dict(expected, orient='index')[cols] |
| 317 | + tm.assert_frame_equal(result, expected) |
0 commit comments