The indices to my DataFrame are datetimes, one row every 10 minutes. I want to set the value of a column based on the hour value of the index. Here is my code so far:
import pandas as pd
df = pd.read_csv('Data.csv', sep='\t');
df['DateTime'] = pd.to_datetime(df['DateTime'])
df.set_index('DateTime', inplace=True)
rng = df.index
day_index = rng[(rng.hour >= 8) & (rng.hour < 22)]
night_index = rng[(rng.hour > 8 ) & (rng.hour <= 22)]
So far so good: the following give the expected parts of the DF:
df.loc[day_index].head()
LAeq
DateTime
2018-01-16 08:00:00 50.0
2018-01-16 08:10:00 49.4
2018-01-16 08:20:00 49.2
2018-01-16 08:30:00 48.9
2018-01-16 08:40:00 48.1
and
df.loc[night_index].head()
LAeq
DateTime
2018-01-15 23:10:00 42.8
2018-01-15 23:20:00 41.9
2018-01-15 23:30:00 43.5
2018-01-15 23:40:00 45.6
2018-01-15 23:50:00 45.7
Shouldn't I be able to set a value for a column for these day and night periods, something like:
df.loc[day_index, 'Limit'] = 55
df.loc[night_index, 'Limit'] = 45
Both give an AttributeError: 'bool' object has no attribute 'any'
What is the simplest way of setting these? I'm making do with the terrible un-Pythonic (warning this code may hurt your eyes):
df['Hour'] = df.index.hour
for i in range(len(df.index)):
h = int(df.ix[i]['Hour'])
if (8 <= h < 22):
df.ix[i, 'Limit'] = 55
else:
df.ix[i,'Limit'] = 45
Which is ugly, but it works.