SoFunction
Updated on 2024-11-15

Explanation of the use of the to_datetime method in the pandas library

Use of pandas to_datetime()

Learning Objectives

Converting timestamp to datetime using pandas

My csv data (where the timesatamp data is the first column):

Use pd.to_datetime() to convert the timestamp value in the first column above to datetime.

The code is as follows

# load data
wireless_df = pd.read_csv("/Users/a123/Desktop/ping_data_lala.csv")

# convert timestamps to datetime
wireless_df["timestamp"] = pd.to_datetime(wireless_df["timestamp"], unit="s")
print(wireless_df["timestamp"])

In the above code pd.to_datetime() has the following parameters

1. each timestamp in wireless_df of type dataframe;

2. unit="s". Explanation: The unix timestamp is the time elapsed since January 1, 1970 (midnight UTC/GMT).secondsThe unit of timestamp is s, so when converting it to datetime, the unit "seconds" is used. Here the parameter unit uses s, not milliseconds or microseconds and other time units.

running result

Date parsing with pandas.to_datetime

import pandas as pd 
df=.read_csv() 
df['Date'] = pd.to_datetime(df['Date'], format='%d/%m/%y')

The above is a personal experience, I hope it can give you a reference, and I hope you can support me more.