Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Separate values from date datatype using Pandas

I wish to separate values from date datatype

Data

time                            ID  
2021-04-16T00:00:00.000-0800    AA
2021-04-23T00:00:00.000-0800    AA
2021-04-30T00:00:00.000-0800    BB

Desired

time        ID  
2021-04-16  AA
2021-04-23  AA
2021-04-30  BB

Doing

df["time"] = df["time"].str.extract(r'(\w+)')

Any suggestion is appreciated

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

A possible solution:

df['time'] = pd.to_datetime(df['time'])
df['time'] = df['time'].dt.date

As @Umar.H suggests — thank you! –, one can use dt.normalize, instead of dt.date, to leave the column time as datetime64:

df['time'] = df['time'].dt.normalize() 

Output:

         time  ID
0  2021-04-16  AA
1  2021-04-23  AA
2  2021-04-30  BB
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading