Skip to content

Instantly share code, notes, and snippets.

@krisbolton
Last active March 17, 2021 21:17
Show Gist options
  • Save krisbolton/aef6179ecf906892565d326b979b3a5b to your computer and use it in GitHub Desktop.
Save krisbolton/aef6179ecf906892565d326b979b3a5b to your computer and use it in GitHub Desktop.
Extract time from datetime in pandas dataframe for TWINT

Problem

I want to extract the time part of a datetime column (2021-02-17 00:14:53). For example, from Twitter data stored in a dataframe. In this example the datetime dtype is stored in a column named date.

Solution

You must convert the column to a datetime using pd.to_datetime and use Pandas dt function to access the time portion of the datetime field.

df['time_only'] = pd.to_datetime(df['date'])
time_only = df['time_only'].dt.time
time_only
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment