A Byte of my 2.2-lb Brain

Just sharing stuff…

Twitter Datetime

Twitter’s “CREATED_AT” format looks something like this Wed Mar 04 02:14:57 +0000 2015. To convert this to Python’s datetime format, simply use datetime.strptime(). If the tweets are stored in a pandas data frame, you may use the following code below to “fix” the formatting (to your liking).

import pandas as pd 
from datetime import datetime

def fixtime(mydate):
	return datetime.strptime(mydate, '%a %b %d %H:%M:%S +0000 %Y')

df = pd.read_csv("mytweets.csv")
df["CREATED_AT"] = df.apply(lambda row: fixtime(row["CREATED_AT"]), axis = 1)

The resulting format becomes a Timestamp data type that looks like this: 2015-03-04 02:14:57

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s


This entry was posted on May 28, 2015 by in Geek and tagged , , .
%d bloggers like this: