Pandas integer YYMMDD to datetime

Given a pandas dataframe, we have to convert integer YYMMDD to datetime. By Pranit Sharma Last updated : October 03, 2023

Pandas is a special tool that allows us to perform complex manipulations of data effectively and efficiently. Inside pandas, we mostly deal with a dataset in the form of DataFrame. DataFrames are 2-dimensional data structures in pandas. DataFrames consist of rows, columns, and data.

Problem statement

We are given a DataFrame where a column contains some large integer values and we need to convert these large integer values into the proper date format of yymmdd.

Converting integer YYMMDD to datetime

For this purpose, we will first convert these large integer values into the string and then we will pass these string values inside pandas.to_datetime() method inside it we will pass a parameter called format which we will assign as yymmdd.

Let us understand with the help of an example,

Python program to convert an integer value YYMMDD to datetime

# Importing pandas package
import pandas as pd

# Importing numpy package
import numpy as np

# Importing minmaxscaler method from sklearn
from sklearn.preprocessing import MinMaxScaler

# Creating DataFrame
df = pd.DataFrame(np.random.randn(5, 3), index=list('abcde'), columns=list('xyz'))

# Display original DataFrame
print("Original DataFrame:\n",df,"\n")

# Creating an object for MinMaxScaler
mms = MinMaxScaler()

# Scaling secific columns
df[['x','z']] = mms.fit_transform(df[['x','z']])

# Display Modified DataFrame
print("Modified DataFrame:\n",df)

Output

The output of the above program is:

Example: Pandas integer YYMMDD to datetime

Python Pandas Programs »

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.