{"action":"created","issue":{"url":"https://api.github.com/repos/pydata/pandas/issues/12859","repository_url":"https://api.github.com/repos/pydata/pandas","labels_url":"https://api.github.com/repos/pydata/pandas/issues/12859/labels{/name}","comments_url":"https://api.github.com/repos/pydata/pandas/issues/12859/comments","events_url":"https://api.github.com/repos/pydata/pandas/issues/12859/events","html_url":"https://github.com/pydata/pandas/issues/12859","id":147407860,"number":12859,"title":"to_dict with outtype='records' mutates the index type","user":{"login":"tsu-shiuan","id":16593552,"avatar_url":"https://avatars.githubusercontent.com/u/16593552?v=3","gravatar_id":"","url":"https://api.github.com/users/tsu-shiuan","html_url":"https://github.com/tsu-shiuan","followers_url":"https://api.github.com/users/tsu-shiuan/followers","following_url":"https://api.github.com/users/tsu-shiuan/following{/other_user}","gists_url":"https://api.github.com/users/tsu-shiuan/gists{/gist_id}","starred_url":"https://api.github.com/users/tsu-shiuan/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/tsu-shiuan/subscriptions","organizations_url":"https://api.github.com/users/tsu-shiuan/orgs","repos_url":"https://api.github.com/users/tsu-shiuan/repos","events_url":"https://api.github.com/users/tsu-shiuan/events{/privacy}","received_events_url":"https://api.github.com/users/tsu-shiuan/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/pydata/pandas/labels/Difficulty%20Novice","name":"Difficulty Novice","color":"fbca04"},{"url":"https://api.github.com/repos/pydata/pandas/labels/Dtypes","name":"Dtypes","color":"e102d8"},{"url":"https://api.github.com/repos/pydata/pandas/labels/Effort%20Low","name":"Effort Low","color":"006b75"}],"state":"open","locked":false,"assignee":null,"milestone":{"url":"https://api.github.com/repos/pydata/pandas/milestones/39","html_url":"https://github.com/pydata/pandas/milestones/0.19.0","labels_url":"https://api.github.com/repos/pydata/pandas/milestones/39/labels","id":1570595,"number":39,"title":"0.19.0","description":"after 0.18.x series","creator":{"login":"jreback","id":953992,"avatar_url":"https://avatars.githubusercontent.com/u/953992?v=3","gravatar_id":"","url":"https://api.github.com/users/jreback","html_url":"https://github.com/jreback","followers_url":"https://api.github.com/users/jreback/followers","following_url":"https://api.github.com/users/jreback/following{/other_user}","gists_url":"https://api.github.com/users/jreback/gists{/gist_id}","starred_url":"https://api.github.com/users/jreback/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jreback/subscriptions","organizations_url":"https://api.github.com/users/jreback/orgs","repos_url":"https://api.github.com/users/jreback/repos","events_url":"https://api.github.com/users/jreback/events{/privacy}","received_events_url":"https://api.github.com/users/jreback/received_events","type":"User","site_admin":false},"open_issues":18,"closed_issues":0,"state":"open","created_at":"2016-02-08T15:30:21Z","updated_at":"2016-04-11T12:31:09Z","due_on":"2016-08-31T04:00:00Z","closed_at":null},"comments":3,"created_at":"2016-04-11T12:10:21Z","updated_at":"2016-04-11T12:37:53Z","closed_at":null,"body":"I'm am trying to call the to_dict function on the following DataFrame:\r\n\r\n```\r\nimport pandas as pd\r\n\r\ndata = {\"a\": [1,2,3,4,5], \"b\": [90,80,40,60,30]}\r\n\r\ndf = pd.DataFrame(data)\r\n```\r\n\r\n```\r\ndf\r\n   a   b\r\n0  1  90\r\n1  2  80\r\n2  3  40\r\n3  4  60\r\n4  5  30\r\n```\r\n\r\n```\r\ndf.reset_index().to_dict(\"r\")\r\n[{'a': 1, 'b': 90, 'index': 0},\r\n {'a': 2, 'b': 80, 'index': 1},\r\n {'a': 3, 'b': 40, 'index': 2},\r\n {'a': 4, 'b': 60, 'index': 3},\r\n {'a': 5, 'b': 30, 'index': 4}]\r\n\r\n```\r\nHowever my problem occurs if I perform a float operation on the dataframe, which mutates the index into a float:\r\n\r\n```\r\n(df*1.0).reset_index().to_dict(\"r\")\r\n[{'a': 1.0, 'b': 90.0, 'index': 0.0},  \r\n{'a': 2.0, 'b': 80.0, 'index': 1.0},  \r\n{'a': 3.0, 'b': 40.0, 'index': 2.0},  \r\n{'a': 4.0, 'b': 60.0, 'index': 3.0},  \r\n{'a': 5.0, 'b': 30.0, 'index': 4.0}]\r\n```\r\n\r\nCan anyone explain the above behaviour or recommend a workaround, or verify whether or not this could be a pandas bug? None of the other outtypes in the to_dict method mutates the index as shown above.\r\n\r\nI've replicated this on both pandas 0.14 and 0.18 (latest)\r\n\r\nMany thanks!\r\n\r\nlink to stackoverflow: http://stackoverflow.com/questions/36548151/pandas-to-dict-changes-index-type-with-outtype-records"},"comment":{"url":"https://api.github.com/repos/pydata/pandas/issues/comments/208319535","html_url":"https://github.com/pydata/pandas/issues/12859#issuecomment-208319535","issue_url":"https://api.github.com/repos/pydata/pandas/issues/12859","id":208319535,"user":{"login":"TomAugspurger","id":1312546,"avatar_url":"https://avatars.githubusercontent.com/u/1312546?v=3","gravatar_id":"","url":"https://api.github.com/users/TomAugspurger","html_url":"https://github.com/TomAugspurger","followers_url":"https://api.github.com/users/TomAugspurger/followers","following_url":"https://api.github.com/users/TomAugspurger/following{/other_user}","gists_url":"https://api.github.com/users/TomAugspurger/gists{/gist_id}","starred_url":"https://api.github.com/users/TomAugspurger/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/TomAugspurger/subscriptions","organizations_url":"https://api.github.com/users/TomAugspurger/orgs","repos_url":"https://api.github.com/users/TomAugspurger/repos","events_url":"https://api.github.com/users/TomAugspurger/events{/privacy}","received_events_url":"https://api.github.com/users/TomAugspurger/received_events","type":"User","site_admin":false},"created_at":"2016-04-11T12:37:53Z","updated_at":"2016-04-11T12:37:53Z","body":"Something like\r\n\r\n```python\r\nIn [28]: [x._asdict() for x in df.itertuples()]\r\nOut[28]:\r\n[OrderedDict([('Index', 0), ('a', 1.0), ('b', 90)]),\r\n OrderedDict([('Index', 1), ('a', 2.0), ('b', 80)]),\r\n OrderedDict([('Index', 2), ('a', 3.0), ('b', 40)]),\r\n OrderedDict([('Index', 3), ('a', 4.0), ('b', 60)]),\r\n OrderedDict([('Index', 4), ('a', 5.0), ('b', 30)])]\r\n```\r\n\r\nThat's an OrderedDict using `namedtuple._asdict`, you can write dict comprehension if you want a regular one."}}