Python Forum
How to get datetime from numeric format field
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to get datetime from numeric format field
#1
Hi there,

Can someone help me to extract datetime from the following "data" field

import pandas as pd
import datetime as dt

df1 = pd.DataFrame(data={"fruit":['apple','pear','banana','peach'],
                          'id':[2,1,3,4],
                          'Data':['20210522113347','20210522113359','20210522113412','20210522113431']})

df1['Data1'] = pd.to_datetime((df1['Data'])).dt.strftime('%Y%M%D%H%M%S')
Output:
fruit id Data Data1 0 apple 2 20210522113347 20213305/22/21113347 1 pear 1 20210522113359 20213305/22/21113359 2 banana 3 20210522113412 20213405/22/21113412 3 peach 4 20210522113431 20213405/22/21113431
Appreciate it if someone can help.
Reply
#2
What are you trying to do? This will give you a column of datetime objects.
df1['Data1'] = pd.to_datetime((df1['Data']))
Now that you have a datetime object you can convert it back to a string. I like to format dates like Nov 5, 2021.
import pandas as pd
import datetime as dt

df = pd.DataFrame(data={"fruit":['apple','pear','banana','peach'],
                          'id':[2,1,3,4],
                          'Date':['20201221113347','20211103113359','20211103113412','20211105113431']})

df['Data1'] = pd.to_datetime((df['Date'])).dt.strftime('%b %d, %Y')
print(df)
Output:
fruit id Date Data1 0 apple 2 20201221113347 Dec 21, 2020 1 pear 1 20211103113359 Nov 03, 2021 2 banana 3 20211103113412 Nov 03, 2021 3 peach 4 20211105113431 Nov 05, 2021
Note: I changed some of your dates . Better toss the apples!

A description of format characters for datetime.strftime:

https://docs.python.org/3/library/dateti...e-behavior

If you want to extract info like the day, month, year etc from the datetime:

https://docs.python.org/3/library/dateti...me-objects
klllmmm likes this post
Reply
#3
Thanks!! @deanhystad
Reply
#4
As info so dos Pandas has Time/date functionality build in.
So no need for this import import datetime as dt
import pandas as pd

df1 = pd.DataFrame(
    data={
        "fruit": ["apple", "pear", "banana", "peach"],
        "id": [2, 1, 3, 4],
        "Data": [
            "20210522113347",
            "20210522113359",
            "20210522113412",
            "20210522113431",
        ],
    }
)

df1['Data'] = pd.to_datetime(df1['Data'])
>>> df1
    fruit  id                Data
0   apple   2 2021-05-22 11:33:47
1    pear   1 2021-05-22 11:33:59
2  banana   3 2021-05-22 11:34:12
3   peach   4 2021-05-22 11:34:31
>>> df1.Data.dt.strftime('%m/%d/%Y')
0    05/22/2021
1    05/22/2021
2    05/22/2021
3    05/22/2021
Name: Data, dtype: object

>>> #Save DataFrame with a new date format
>>> df1['Data'] = df1.Data.dt.strftime('%b %d, %Y')
>>> df1
    fruit  id          Data
0   apple   2  May 22, 2021
1    pear   1  May 22, 2021
2  banana   3  May 22, 2021
3   peach   4  May 22, 2021
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Numeric Enigma Machine idev 8 241 Yesterday, 02:17 PM
Last Post: idev
Question Numeric Anagrams - Count Occurances monty024 2 1,475 Nov-13-2021, 05:05 PM
Last Post: monty024
  Exporting dataframes to excel without loosing datetime format Rafa 0 1,212 Oct-27-2021, 10:42 AM
Last Post: Rafa
  Extract continuous numeric characters from a string in Python Robotguy 2 2,583 Jan-16-2021, 12:44 AM
Last Post: snippsat
  Bug ? when dataclass field name == field type Cyril 0 1,526 Oct-22-2020, 03:26 AM
Last Post: Cyril
  How to calculate column mean and row skip non numeric and na Mekala 5 4,835 May-06-2020, 10:52 AM
Last Post: anbu23
  Datetime format issue with z sks3286 2 7,324 Apr-07-2020, 12:26 PM
Last Post: sks3286
  Alpha numeric element list search rhubarbpieguy 1 1,745 Apr-01-2020, 12:41 PM
Last Post: pyzyx3qwerty
  convert a character to numeric and back Skaperen 2 2,070 Jan-28-2020, 09:32 PM
Last Post: Skaperen
  are numeric types passed by value or reference? rudihammad 4 2,560 Nov-19-2019, 06:25 AM
Last Post: rudihammad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020