"between()" function with only right or left inclusive
Question:
all I am trying to filter some dates with pandas.
I initially tried doing that with this code
bd = pd.DataFrame({'year': [2018, 2018], # billing data
'month': [9, 10],
'day': [14, 15]})
bd = pd.to_datetime(bd)
month1 = (df2.Date >= bd[0]) & (df.Date < bd[1])
I would get the error of TypeError: Cannot compare type ‘Timestamp’ with type ‘str’
However, when I did the between() function
bd = pd.DataFrame({'year': [2018, 2018],
'month': [9, 10],
'day': [14, 15]})
bd = pd.to_datetime(bd)
month1 = df2[df2['Date'].between(bd[0], bd[1])]
It worked, but it would include the 15th of October which I do not want. The between() function is useful, but I would like to now what alternatives I have if I only need one side inclusive, and the other exclusive. Thank you for the help.
Answers:
The fix for between
is simple. You subtract one day on the side which you want the ‘exclusive’. In your case, it is like this
a_day = pd.DateOffset(1)
month1 = df2[df2['Date'].between(bd[0], bd[1] - a_day)
Otherwise, if you really want totally control on the inclusiveness of sides, you need to use pd.IntervalArray
or pd.IntervalIndex
I think between also supports a parameter called inclusive.
month1 = df2[df2['Date'].between(bd[0], bd[1], inclusive='left')]
all I am trying to filter some dates with pandas.
I initially tried doing that with this code
bd = pd.DataFrame({'year': [2018, 2018], # billing data
'month': [9, 10],
'day': [14, 15]})
bd = pd.to_datetime(bd)
month1 = (df2.Date >= bd[0]) & (df.Date < bd[1])
I would get the error of TypeError: Cannot compare type ‘Timestamp’ with type ‘str’
However, when I did the between() function
bd = pd.DataFrame({'year': [2018, 2018],
'month': [9, 10],
'day': [14, 15]})
bd = pd.to_datetime(bd)
month1 = df2[df2['Date'].between(bd[0], bd[1])]
It worked, but it would include the 15th of October which I do not want. The between() function is useful, but I would like to now what alternatives I have if I only need one side inclusive, and the other exclusive. Thank you for the help.
The fix for between
is simple. You subtract one day on the side which you want the ‘exclusive’. In your case, it is like this
a_day = pd.DateOffset(1)
month1 = df2[df2['Date'].between(bd[0], bd[1] - a_day)
Otherwise, if you really want totally control on the inclusiveness of sides, you need to use pd.IntervalArray
or pd.IntervalIndex
I think between also supports a parameter called inclusive.
month1 = df2[df2['Date'].between(bd[0], bd[1], inclusive='left')]