<jupyter_start><jupyter_text>use date column as index<jupyter_code>datetime_series = pd.to_datetime(df['date_of_birth'])

datetime_index = pd.DatetimeIndex(datetime_series.values)

df2=df.set_index(datetime_index)

df2.drop('date_of_birth',axis=1,inplace=True)

df2
df2.index<jupyter_output><empty_output><jupyter_text>fill empty periods<jupyter_code>df = pd.DataFrame({
    'name':[
        'john','mary','peter','jeff','bill'
    ],
    'year_born':[
        '2000', '1999', '2001', '1995', '1992',
    ],
})
df
df.index
datetime_series = pd.to_datetime(df['year_born'])

datetime_index = pd.DatetimeIndex(datetime_series.values)

df3=df.set_index(datetime_index)

df3.drop('year_born',axis=1,inplace=True)

df3.sort_index(inplace=True)

df3
df3.index
df4=df3.asfreq('YS')
df4
df4.index<jupyter_output><empty_output><jupyter_text>lag columns<jupyter_code>df = pd.DataFrame(
    data={'reading': np.random.uniform(high=100,size=10)},
    index=pd.to_datetime([date(2019,1,d) for d in range(1,11)])
)
df
df['reading_d_minus_1']=df['reading'].shift(1,freq='D')

df['reading_d_minus_2']=df['reading'].shift(2,freq='D')
df<jupyter_output><empty_output><jupyter_text>plot data aggregated by day<jupyter_code>from datetime import date,datetime,timedelta

import matplotlib.pyplot as plt
import matplotlib.ticker as mtick
import numpy as np
import pandas as pd

np.random.seed(42)

# create a dummy dataset
df = pd.DataFrame(
    index=pd.to_datetime(
        [datetime(2019,1,1)+timedelta(hours=h+10*np.random.uniform()) for h in range(0,1000)]
    ),
    data={
        'browser': np.random.choice([
            'chrome',
            'firefox',
            'safari',
            'edge'
        ],size=1000,replace=True)
    },
    
)

print(type(df.index))
# 'pandas.core.indexes.datetimes.DatetimeIndex'

# just to make it more intuitive
df.index.name= 'login_datetime'

df.groupby(by=[lambda dt: dt.strftime('%Y-%m-%d'),'browser']).size().groupby(level=0).apply(
    lambda x: 100 * x / x.sum()
).unstack().plot(kind='bar',stacked=True,legend='reverse',width=0.8)

current_handles, current_labels = plt.gca().get_legend_handles_labels()

# sort or reorder the labels and handles
reversed_handles = list(reversed(current_handles))
reversed_labels = list(reversed(current_labels))

# call plt.legend() with the new values
plt.legend(reversed_handles,reversed_labels,loc='center left', bbox_to_anchor=(1, 0.5))
plt.gca().yaxis.set_major_formatter(mtick.PercentFormatter())
plt.gcf().set_size_inches(12,5)
plt.title('Agreggated number of visits to website, per day and browser')
plt.show()<jupyter_output><class 'pandas.core.indexes.datetimes.DatetimeIndex'>
{'fraction_non_alphanumeric': 0.1568627450980392, 'fraction_numerical': 0.03129713423831071, 'mean_word_length': 5.6325, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '417203', 'license': 'MIT', 'n_tokens_mistral': 1066, 'n_tokens_neox': 997, 'n_words': 151, 'path': 'python3/notebooks/pandas-datetimeindex/datetime-index.ipynb', 'repo_name': 'tobby2002/python-sandbox', 'size': '2652'}