Main idea is to beat your date/time columns into datetime
objects, and then do the date filtering with those objects. I assumed they were string to start.
I'm not sure how your dates are incoming, but hopefully you can figure out how to create datetime
objects out of them. If not, let me know, and we can update. Here's an example of how you could filter by date and sum the values over a start/end date:
import numpy as np
import pandas as pd
import datetime
cols = ["Date", "Time", "Value"]
rows = [["29-Jan-18", "11:00", 10],
["29-Jan-18", "12:00", 11],
["29-Jan-18", "13:00", 12],
["29-Jan-18", "14:00", 13],
["31-Jan-18", "11:00", 90],
["31-Jan-18", "12:00", 91],
["31-Jan-18", "13:00", 92],
["31-Jan-18", "14:00", 93]]
df = pd.DataFrame(rows, columns=cols)
df['datetime_str'] = df['Date'].str.cat(df['Time'].astype(str), sep=" ")
df['datetime'] = df['datetime_str'].apply(lambda d: datetime.datetime.strptime(d, '%d-%b-%y %H:%M'))
print(df)
print()
# Hopefully you can figure out how to beat incoming start/end date into datetime.
# If not, let me know how the start/end are coming in and we can write a function to do it
start_date = datetime.datetime.strptime("29-Jan-18 11:00", '%d-%b-%y %H:%M')
end_date = datetime.datetime.strptime("29-Jan-18 13:00", '%d-%b-%y %H:%M')
value_sum = df[ (df['datetime'] >= start_date) & (df['datetime'] <= end_date) ]['Value'].sum()
print("Value sum from " + str(start_date) + " to " + str(end_date) + ": ", value_sum)
# Works accross days as well
start_date = datetime.datetime.strptime("29-Jan-18 13:00", '%d-%b-%y %H:%M')
end_date = datetime.datetime.strptime("31-Jan-18 13:00", '%d-%b-%y %H:%M')
value_sum = df[ (df['datetime'] >= start_date) & (df['datetime'] <= end_date) ]['Value'].sum()
print("Value sum from " + str(start_date) + " to " + str(end_date) + ": ", value_sum)
This outputs:
Date Time Value datetime_str datetime
0 29-Jan-18 11:00 10 29-Jan-18 11:00 2018-01-29 11:00:00
1 29-Jan-18 12:00 11 29-Jan-18 12:00 2018-01-29 12:00:00
2 29-Jan-18 13:00 12 29-Jan-18 13:00 2018-01-29 13:00:00
3 29-Jan-18 14:00 13 29-Jan-18 14:00 2018-01-29 14:00:00
4 31-Jan-18 11:00 90 31-Jan-18 11:00 2018-01-31 11:00:00
5 31-Jan-18 12:00 91 31-Jan-18 12:00 2018-01-31 12:00:00
6 31-Jan-18 13:00 92 31-Jan-18 13:00 2018-01-31 13:00:00
7 31-Jan-18 14:00 93 31-Jan-18 14:00 2018-01-31 14:00:00
Value sum from 2018-01-29 11:00:00 to 2018-01-29 13:00:00: 33
Value sum from 2018-01-29 13:00:00 to 2018-01-31 13:00:00: 298