Skip to content

Latest commit

 

History

History
71 lines (48 loc) · 2.29 KB

Pandas_Convert_datetime_series.md

File metadata and controls

71 lines (48 loc) · 2.29 KB



Template request | Bug report | Generate Data Product

Tags: #pandas #python #date #conversion #operations #snippet

Author: Florent Ravenel

Description: This notebook provides instructions on how to use the Pandas library to convert a datetime series into a usable format.

References:

Input

Import libraries

import pandas as pd

Setup Variables

To parse your date format : you can use the d3 time format documentation

  • current_format: Your date string format
  • new_format: New date format you want to use
# Your date string format
current_format = "%d/%m/%Y"

# New date format you want to use
new_format = "%Y-W%U"

Model

Create Dataframe

dict1 = {
    "Name": ["Peter", "Dolly", "Maggie", "David", "Isabelle"],
    "Date": ["20/2/2021", "19/8/2014", "8/9/2000", "4/3/2013", "14/7/1995"],
    "Second Date": [
        "August 20,2011",
        "September 16,1993",
        "January 23,2009",
        "October 17,2019",
        "March 4,2021",
    ],
}
df = pd.DataFrame(dict1)
df

Convert datetime string series to datetime series to another datetime string

df["New_Date"] = pd.to_datetime(df["Date"], format=current_format).dt.strftime(new_format)
df

Output

Display new DataFrame

df