
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Write a program to separate date and time from the datetime column in Python Pandas
Assume, you have datetime column in dataframe and the result for separating date and time as,
datetime date time 0 2020-01-01 07:00:00 2020-01-06 07:00:00 1 2020-01-02 07:00:00 2020-01-06 07:00:00 2 2020-01-03 07:00:00 2020-01-06 07:00:00 3 2020-01-04 07:00:00 2020-01-06 07:00:00 4 2020-01-05 07:00:00 2020-01-06 07:00:00 5 2020-01-06 07:00:00 2020-01-06 07:00:00
To solve this, we will follow the below approaches −
Solution 1
Define a dataframe ‘datetime’ column using pd.date_range(). It is defined below,
pd.DataFrame({'datetime':pd.date_range('2020-01-01 07:00',periods=6)})
Set for loop d variable to access df[‘datetime’] column one by one.
Convert date and time from for loop and save it as df[‘date’] and df[‘time’]. It is defined below,
for d in df['datetime']: df['date'] = d.date() df['time'] = d.time()
Example
Let’s check the following code to get a better understanding −
import pandas as pd df = pd.DataFrame({'datetime':pd.date_range('2020-01-01 07:00',periods=6)}) print("DataFrame is:\n", df) for d in df['datetime']: df['date'] = d.date() df['time'] = d.time() print(df)
Output
DataFrame is: datetime 0 2020-01-01 07:10:00 1 2020-01-02 07:10:00 2 2020-01-03 07:10:00 3 2020-01-04 07:10:00 4 2020-01-05 07:10:00 5 2020-01-06 07:10:00 Date-time-hour-minutes : datetime date time 0 2020-01-01 07:10:00 2020-01-06 07:10:00 1 2020-01-02 07:10:00 2020-01-06 07:10:00 2 2020-01-03 07:10:00 2020-01-06 07:10:00 3 2020-01-04 07:10:00 2020-01-06 07:10:00 4 2020-01-05 07:10:00 2020-01-06 07:10:00 5 2020-01-06 07:10:00 2020-01-06 07:10:00
Solution 2
Define a dataframe
Apply pd.to_datetime() function inside df[‘datetime’] and select date using dt.date then save it as df[‘date’]
Apply pd.to_datetime() function inside df[‘datetime’] and select time using dt.time then save it as df[‘time’]
Example
Let’s check the following code to get a better understanding −
import pandas as pd df = pd.DataFrame({'datetime':pd.date_range('2020-01-01 07:10',periods=6)}) print("DataFrame is:\n", df) df['date'] = pd.to_datetime(df['datetime']).dt.date df['time'] = pd.to_datetime(df['datetime']).dt.time print("Date-time-hour-minutes :\n", df)
Output
DataFrame is: datetime 0 2020-01-01 07:10:00 1 2020-01-02 07:10:00 2 2020-01-03 07:10:00 3 2020-01-04 07:10:00 4 2020-01-05 07:10:00 5 2020-01-06 07:10:00 Date-time-hour-minutes : datetime date time 0 2020-01-01 07:10:00 2020-01-06 07:10:00 1 2020-01-02 07:10:00 2020-01-06 07:10:00 2 2020-01-03 07:10:00 2020-01-06 07:10:00 3 2020-01-04 07:10:00 2020-01-06 07:10:00 4 2020-01-05 07:10:00 2020-01-06 07:10:00 5 2020-01-06 07:10:00 2020-01-06 07:10:00
- Related Articles
- Concatenate date and time from separate columns into a single column in MySQL
- Create DATETIME from DATE and TIME in MySQL?
- How to part DATE and TIME from DATETIME in MySQL?
- How to split the datetime column into date and time and compare individually in MySQL?
- Python Pandas - Get the current date and time from Timestamp object
- How to compare DateTime Column with only Date not time in MySQL?
- How to select only MySQL date from datetime column?
- Pandas program to convert a string of date into time
- How to combine date and time from different MySQL columns to compare with the entire DateTime?
- How to extract from datetime column in MySQL by comparing only date and ignoring whitespace?
- Add DATE and TIME fields to get DATETIME field in MySQL?
- Write a program in Python Pandas to convert a dataframe Celsius data column into Fahrenheit
- How to convert JS date time to MySQL datetime?
- Python Pandas - Construct a naive UTC datetime from a POSIX timestamp
- How to convert date to datetime in Python?
