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
#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


Messages In This Thread
RE: How to get datetime from numeric format field - by snippsat - Nov-06-2021, 03:26 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Numeric Enigma Machine idev 9 1,018 Mar-29-2024, 06:15 PM
Last Post: idev
Question Numeric Anagrams - Count Occurances monty024 2 1,641 Nov-13-2021, 05:05 PM
Last Post: monty024
  Exporting dataframes to excel without loosing datetime format Rafa 0 1,314 Oct-27-2021, 10:42 AM
Last Post: Rafa
  Extract continuous numeric characters from a string in Python Robotguy 2 2,800 Jan-16-2021, 12:44 AM
Last Post: snippsat
  Bug ? when dataclass field name == field type Cyril 0 1,644 Oct-22-2020, 03:26 AM
Last Post: Cyril
  How to calculate column mean and row skip non numeric and na Mekala 5 5,242 May-06-2020, 10:52 AM
Last Post: anbu23
  Datetime format issue with z sks3286 2 7,657 Apr-07-2020, 12:26 PM
Last Post: sks3286
  Alpha numeric element list search rhubarbpieguy 1 1,900 Apr-01-2020, 12:41 PM
Last Post: pyzyx3qwerty
  convert a character to numeric and back Skaperen 2 2,232 Jan-28-2020, 09:32 PM
Last Post: Skaperen
  are numeric types passed by value or reference? rudihammad 4 2,785 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