Solution
You could try this:
df.loc[df.TRANSACTION_DT.between('2020-07-03', '2020-07-08', inclusive=False)]
In a nutshell, you could try any of the following methods. I personally find Method-2 as the most versatile one. In case you are only interested to find dates between two given dates, you could also use Method-1 (pd.Series.between()
).
# import numpy as np
# import pandas as pd
# start = '2020-07-03'
# end = '2020-07-08'
# One line solution with pd.DataFrame.where()
# Method-1
df.where(df.TRANSACTION_DT.between(start, end, inclusive=False))
# Method-2
df.where((df.TRANSACTION_DT > start) & (df.TRANSACTION_DT < end))
# Method-3
df.where(np.logical_and((df.TRANSACTION_DT > start), (df.TRANSACTION_DT < end)))
For example, if you use Method-1, and only want to see which rows in the dataframe satisfy the filtering condition(s), you could do it as follows:
# Note: I used the DUMMY DATA created
# in the following section for df
df.loc[df.TRANSACTION_DT.between(start, end, inclusive=False)]
## Output:
#
# TRANSACTION_DT Method_1 Method-2
# 3 2020-07-04 True True
# 4 2020-07-05 True True
# 5 2020-07-06 True True
# 6 2020-07-07 True True
Example: With Dummy Data
import numpy as np
import pandas as pd
start = '2020-07-03'
end = '2020-07-08'
## Dummy Data
dates = pd.date_range(start='2020-07-01', end='2020-07-10', freq='D')
df = pd.DataFrame(dates, columns=['TRANSACTION_DT'])
## Apply Criterion
# Method-1
df['Method_1'] = df.TRANSACTION_DT.between(start, end, inclusive=False)
# Method-2
df['Method_2'] = (df.TRANSACTION_DT > start) & (df.TRANSACTION_DT < end)
print(df)
Output:
TRANSACTION_DT Method_1 Method_2
0 2020-07-01 False False
1 2020-07-02 False False
2 2020-07-03 False False
3 2020-07-04 True True
4 2020-07-05 True True
5 2020-07-06 True True
6 2020-07-07 True True
7 2020-07-08 False False
8 2020-07-09 False False
9 2020-07-10 False False