Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
Write a program to separate date and time from the datetime column in Python Pandas
When working with datetime data in Pandas, you often need to separate date and time components into different columns. This is useful for data analysis, filtering, and visualization purposes.
Using the dt Accessor (Recommended)
The most efficient approach is using Pandas' dt accessor to extract date and time components directly ?
import pandas as pd
# Create sample DataFrame with datetime column
df = pd.DataFrame({'datetime': pd.date_range('2020-01-01 07:00', periods=6)})
print("Original DataFrame:")
print(df)
# Extract date and time using dt accessor
df['date'] = df['datetime'].dt.date
df['time'] = df['datetime'].dt.time
print("\nAfter separating date and time:")
print(df)
Original DataFrame:
datetime
0 2020-01-01 07:00:00
1 2020-01-02 07:00:00
2 2020-01-03 07:00:00
3 2020-01-04 07:00:00
4 2020-01-05 07:00:00
5 2020-01-06 07:00:00
After separating date and time:
datetime date time
0 2020-01-01 07:00:00 2020-01-01 07:00:00
1 2020-01-02 07:00:00 2020-01-02 07:00:00
2 2020-01-03 07:00:00 2020-01-03 07:00:00
3 2020-01-04 07:00:00 2020-01-04 07:00:00
4 2020-01-05 07:00:00 2020-01-05 07:00:00
5 2020-01-06 07:00:00 2020-01-06 07:00:00
Using String Formatting
You can also extract date and time as formatted strings for display purposes ?
import pandas as pd
df = pd.DataFrame({'datetime': pd.date_range('2020-01-01 07:00', periods=4)})
# Extract as formatted strings
df['date_str'] = df['datetime'].dt.strftime('%Y-%m-%d')
df['time_str'] = df['datetime'].dt.strftime('%H:%M:%S')
print(df)
datetime date_str time_str
0 2020-01-01 07:00:00 2020-01-01 07:00:00
1 2020-01-02 07:00:00 2020-01-02 07:00:00
2 2020-01-03 07:00:00 2020-01-03 07:00:00
3 2020-01-04 07:00:00 2020-01-04 07:00:00
Working with String Datetime Data
If your datetime column contains strings, first convert it to datetime format ?
import pandas as pd
# Create DataFrame with string datetime
df = pd.DataFrame({
'datetime_str': ['2020-01-01 09:30:15', '2020-01-02 14:45:30', '2020-01-03 18:20:45']
})
# Convert to datetime first
df['datetime'] = pd.to_datetime(df['datetime_str'])
# Extract date and time
df['date'] = df['datetime'].dt.date
df['time'] = df['datetime'].dt.time
print(df[['datetime', 'date', 'time']])
datetime date time
0 2020-01-01 09:30:15 2020-01-01 09:30:15
1 2020-01-02 14:45:30 2020-01-02 14:45:30
2 2020-01-03 18:20:45 2020-01-03 18:20:45
Comparison of Methods
| Method | Output Type | Best For |
|---|---|---|
dt.date |
Date object | Date calculations and filtering |
dt.time |
Time object | Time calculations and filtering |
dt.strftime() |
String | Display and formatting |
Conclusion
Use the dt accessor with .date and .time for extracting date and time components from datetime columns. This method is vectorized and efficient for large datasets. For formatted output, use strftime() with appropriate format codes.
Advertisements
