Dateint
It's very common to store date/datetimes as integers or strings using formats
such as YYYYmmdd or YYYYmm. In python, to perform date/datetime arithmetic on those
values, one needs to:
- convert the original value to
dateordatetime - perform the date/datetime operation
- convert the result back to the original format
With dateint, we abstract all convertion operations so you can focus on the
arithmetic step:
-
single value:
-
pandas:
Installation
Examples
Add a time interval to a date
import dateint as di
import pandas as pd
di.add(20220510, days=15)
# 20220525
dates = pd.Series([202201, 202202, 202203])
di.add(dates, months=2)
'''
0 202203
1 202204
2 202205
dtype: int64
'''
Subtract a time interval from a date
import dateint as di
import pandas as pd
di.sub(20220510, days=8)
# 20220502
dates = pd.Series([202201, 202202, 202203])
di.sub(dates, years=1)
'''
0 202101
1 202102
2 202103
dtype: int64
'''
Day of week
import dateint as di
import pandas as pd
di.sub(20220510, days=8)
# 20220502
df = pd.DataFrame(
{
'date': [
20220103,
20220104,
20220105,
20220106,
20220107,
20220108,
20220109,
]
}
)
df['weekday'] = di.weekday(df['date'])
df
'''
date weekday
0 20220103 0
1 20220104 1
2 20220105 2
3 20220106 3
4 20220107 4
5 20220108 5
6 20220109 6
'''
License
This project is licensed under the terms of the MIT license.