{"action":"created","issue":{"url":"https://api.github.com/repos/pydata/pandas/issues/13260","repository_url":"https://api.github.com/repos/pydata/pandas","labels_url":"https://api.github.com/repos/pydata/pandas/issues/13260/labels{/name}","comments_url":"https://api.github.com/repos/pydata/pandas/issues/13260/comments","events_url":"https://api.github.com/repos/pydata/pandas/issues/13260/events","html_url":"https://github.com/pydata/pandas/issues/13260","id":156337688,"number":13260,"title":"Strange behavior when combining astype and loc","user":{"login":"rladeira","id":8516338,"avatar_url":"https://avatars.githubusercontent.com/u/8516338?v=3","gravatar_id":"","url":"https://api.github.com/users/rladeira","html_url":"https://github.com/rladeira","followers_url":"https://api.github.com/users/rladeira/followers","following_url":"https://api.github.com/users/rladeira/following{/other_user}","gists_url":"https://api.github.com/users/rladeira/gists{/gist_id}","starred_url":"https://api.github.com/users/rladeira/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/rladeira/subscriptions","organizations_url":"https://api.github.com/users/rladeira/orgs","repos_url":"https://api.github.com/users/rladeira/repos","events_url":"https://api.github.com/users/rladeira/events{/privacy}","received_events_url":"https://api.github.com/users/rladeira/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"milestone":null,"comments":1,"created_at":"2016-05-23T18:37:22Z","updated_at":"2016-05-23T18:59:00Z","closed_at":"2016-05-23T18:59:00Z","body":"I am trying to use `astype` together with `loc` to convert just a subset of columns to a specified dtype. However, I can't understand what is actually happening. Is this the expected behavior when combining \r\n`astype` and `loc`?\r\n\r\n#### Code Sample, a copy-pastable example if possible\r\n\r\n```\r\nimport pandas as pd\r\nimport numpy as np\r\n\r\ndf = pd.DataFrame({'a': [1,2,3], 'b': [4,5,6], 'c': [7, 8, 9]})\r\n\r\nprint df.loc[:, ['a', 'b']].astype(np.uint8).dtypes\r\n\r\ndf.loc[:, ['a', 'b']] = df.loc[:, ['a', 'b']].astype(np.uint8)\r\nprint df.dtypes\r\n```\r\n\r\nOutput:\r\n```\r\n>>> df.loc[:, ['a', 'b']].astype(np.uint8).dtypes\r\na    uint8\r\nb    uint8\r\ndtype: object\r\n>>> df.loc[:, ['a', 'b']] = df.loc[:, ['a', 'b']].astype(np.uint8)\r\n>>> df.dtypes\r\na    int64\r\nb    int64\r\nc    int64\r\ndtype: object\r\n```\r\n\r\n#### Expected Output\r\n```\r\n>>> df.loc[:, ['a', 'b']].astype(np.uint8).dtypes\r\na    uint8\r\nb    uint8\r\ndtype: object\r\n>>> df.loc[:, ['a', 'b']] = df.loc[:, ['a', 'b']].astype(np.uint8)\r\n>>> df.dtypes\r\na    uint8\r\nb    uint8\r\nc    int64\r\ndtype: object\r\n```\r\n\r\n#### output of ``pd.show_versions()``\r\n\r\nINSTALLED VERSIONS\r\n------------------\r\ncommit: None\r\npython: 2.7.6.final.0\r\npython-bits: 64\r\nOS: Linux\r\nOS-release: 4.2.0-36-generic\r\nmachine: x86_64\r\nprocessor: x86_64\r\nbyteorder: little\r\nLC_ALL: None\r\nLANG: en_US.UTF-8\r\n\r\npandas: 0.18.0\r\nnose: None\r\npip: 8.1.1\r\nsetuptools: 20.6.7\r\nCython: None\r\nnumpy: 1.11.0\r\nscipy: 0.17.0\r\nstatsmodels: None\r\nxarray: None\r\nIPython: 4.1.2\r\nsphinx: None\r\npatsy: None\r\ndateutil: 2.5.2\r\npytz: 2016.3\r\nblosc: None\r\nbottleneck: None\r\ntables: None\r\nnumexpr: None\r\nmatplotlib: 1.5.1\r\nopenpyxl: None\r\nxlrd: None\r\nxlwt: None\r\nxlsxwriter: None\r\nlxml: None\r\nbs4: None\r\nhtml5lib: None\r\nhttplib2: None\r\napiclient: None\r\nsqlalchemy: None\r\npymysql: None\r\npsycopg2: None\r\njinja2: 2.8\r\nboto: None\r\n"},"comment":{"url":"https://api.github.com/repos/pydata/pandas/issues/comments/221063068","html_url":"https://github.com/pydata/pandas/issues/13260#issuecomment-221063068","issue_url":"https://api.github.com/repos/pydata/pandas/issues/13260","id":221063068,"user":{"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},"created_at":"2016-05-23T18:59:00Z","updated_at":"2016-05-23T18:59:00Z","body":"This is as expected; use getitem if you want to re-assign w/o masking. In this case your mask is ':' (meaning the entire columns), but upcasting is done when used in this way (and that's why its upcast to ``int64``).\r\n\r\n```\r\nIn [1]: df = pd.DataFrame({'a': [1,2,3], 'b': [4,5,6], 'c': [7, 8, 9]})\r\n\r\nIn [2]: df\r\nOut[2]: \r\n   a  b  c\r\n0  1  4  7\r\n1  2  5  8\r\n2  3  6  9\r\n\r\nIn [3]: df[['a','b']] = df[['a','b']].astype(np.uint8)\r\n\r\nIn [4]: df\r\nOut[4]: \r\n   a  b  c\r\n0  1  4  7\r\n1  2  5  8\r\n2  3  6  9\r\n\r\nIn [5]: df.dtypes\r\nOut[5]: \r\na    uint8\r\nb    uint8\r\nc    int64\r\ndtype: object\r\n```"}}
{"actor":{"display_login":"jreback"}}