I want to count the number of unqiue days within a pandas dataframe column contianing datatime dtypes. The use-case is Twitter data from TWINT, "How many days has a user tweeted within the collected time period?". The code below assumes the datetime column is called date.
You must first convert the datetime column using pd.to_datetime
and use pandas dt
to access the date portion of the date time. This can then be used by the unique()
and len()
function to count unique occurances.
df['date_only'] = pd.to_datetime(df['date'])
date_only = df['date_only'].dt.date
num_days = len(date_only.unique())
num_days