# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: feml
#     language: python
#     name: feml
# ---

import pandas as pd

# +
# let's create a toy dataframe with some date variables

# first we create a series with the ranges
rng_ = pd.date_range('2019-03-05', periods=20, freq='T')

# now we convert the series in a dataframe
df = pd.DataFrame({'date': rng_}) 

# output the first 5 rows
df.head()

# +
# let's explore the variable type

df.dtypes

# +
# let's extract the date part

df['date_part'] = df['date'].dt.date

df['date_part'].head()

# +
# let's extract the time part

df['time_part'] = df['date'].dt.time

df['time_part'].head()

# +
# let's create a toy dataframe where the datetime variable is cast
# as object

df = pd.DataFrame({'date_var':['Jan-2015', 'Apr-2013', 'Jun-2014', 'Jan-2015']})
df

# +
# let's explore the variable type

df.dtypes

# +
# let's re-cast the variable as datetime

df['datetime_var'] = pd.to_datetime(df['date_var'])
df

# +
# let's extract date and time

df['date'] = df['datetime_var'].dt.date
df['time'] = df['datetime_var'].dt.time

df

# +
# let's explore the variable types

df.dtypes

StarcoderdataJupyterScriptsDedupFiltered