Skip to content

Assign back converted multiple columns to datetime failed #20511

Closed
@jesrael

Description

@jesrael

I want convert multiple columns to datetimes:

df = pd.DataFrame(np.random.choice(['2015-01-01','2016-01-01'], size=(5,6))).add_prefix('date')
print (df)
        date0       date1       date2       date3       date4       date5
0  2015-01-01  2016-01-01  2015-01-01  2015-01-01  2016-01-01  2016-01-01
1  2016-01-01  2016-01-01  2016-01-01  2015-01-01  2016-01-01  2015-01-01
2  2015-01-01  2015-01-01  2016-01-01  2016-01-01  2016-01-01  2015-01-01
3  2016-01-01  2015-01-01  2015-01-01  2015-01-01  2015-01-01  2015-01-01
4  2015-01-01  2016-01-01  2015-01-01  2016-01-01  2016-01-01  2015-01-01

i= range(0,6,2)
df.iloc[:,i] = df.iloc[:,i].apply(lambda x: pd.to_datetime(x, errors='coerce'))
print (df.dtypes)
date0    object
date1    object
date2    object
date3    object
date4    object
date5    object
dtype: object

print (df)
                 date0       date1                date2       date3  \
0  1420070400000000000  2016-01-01  1420070400000000000  2015-01-01   
1  1451606400000000000  2016-01-01  1451606400000000000  2015-01-01   
2  1420070400000000000  2015-01-01  1451606400000000000  2016-01-01   
3  1451606400000000000  2015-01-01  1420070400000000000  2015-01-01   
4  1420070400000000000  2016-01-01  1420070400000000000  2016-01-01   

                 date4       date5  
0  1451606400000000000  2016-01-01  
1  1451606400000000000  2015-01-01  
2  1451606400000000000  2015-01-01  
3  1420070400000000000  2015-01-01  
4  1451606400000000000  2015-01-01  

I test converting:

print (df.iloc[:,i].apply(lambda x: pd.to_datetime(x, errors='coerce')).dtypes)
date0    datetime64[ns]
date2    datetime64[ns]
date4    datetime64[ns]
dtype: object

But assign back convert datetimes to unix dates. Also I test loc and same problem.

print (pd.show_versions())

INSTALLED VERSIONS
------------------
commit: None
python: 3.5.1.final.0
python-bits: 64
OS: Windows
OS-release: 7
machine: AMD64
processor: Intel64 Family 6 Model 60 Stepping 3, GenuineIntel
byteorder: little
LC_ALL: None
LANG: sk_SK
LOCALE: None.None

pandas: 0.22.0
pytest: 2.8.5
pip: 9.0.1
setuptools: 38.4.0
Cython: 0.23.4
numpy: 1.12.1
scipy: 0.19.0
pyarrow: 0.7.0
xarray: None
IPython: 4.1.2
sphinx: 1.3.1
patsy: 0.4.1
dateutil: 2.5.1
pytz: 2016.2
blosc: None
bottleneck: 1.2.1
tables: 3.2.2
numexpr: 2.6.2
feather: 0.4.0
matplotlib: 2.0.0
openpyxl: 2.4.8
xlrd: 0.9.4
xlwt: 1.0.0
xlsxwriter: 0.8.4
lxml: 3.6.0
bs4: 4.6.0
html5lib: 0.999
sqlalchemy: 1.0.12
pymysql: None
psycopg2: None
jinja2: 2.8
s3fs: None
fastparquet: None
pandas_gbq: None
pandas_datareader: 0.4.0
None

Metadata

Metadata

Assignees

Labels

IndexingRelated to indexing on series/frames, not to indexes themselvesNeeds TestsUnit test(s) needed to prevent regressionsgood first issue

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions