alexmulo
alexmulo

Reputation: 791

Count of work hours from time series using pandas

I have a csv file extracted from my windows pc:

date
16/07/2014 09:15:28
16/07/2014 09:15:22
16/07/2014 09:14:56
16/07/2014 09:14:50
16/07/2014 09:14:49
16/07/2014 09:14:46
16/07/2014 09:14:46
16/07/2014 09:14:46
16/07/2014 09:14:46
16/07/2014 09:14:46
16/07/2014 09:14:46
16/07/2014 09:14:46
15/07/2014 14:41:56
15/07/2014 14:41:47
15/07/2014 14:41:30
15/07/2014 14:39:58
15/07/2014 14:39:57
15/07/2014 14:39:54
15/07/2014 14:39:53
15/07/2014 14:39:49

I'd like to count the work hours per day but I did not find any function to split the time from the date and group it. Do you have any idea how to solve it? I also looked for it in google but I only found the count of objects.

Thanks a lot.

Upvotes: 0

Views: 1448

Answers (1)

EdChum
EdChum

Reputation: 394189

Firstly your date values are strings, you can either convert it after loading:

df['date'] = pd.to_datetime(df['date'])

or better is to load it in as datetime in the first place:

In [144]:

df = pd.read_csv('time.csv', parse_dates=[0])
# now extract the hour by applying a lambda and accessing the hour attribute
df['hour']  = df['date'].apply(lambda x: x.hour)
df
Out[144]:
                  date  hour
0  2014-07-16 09:15:28     9
1  2014-07-16 09:15:22     9
2  2014-07-16 09:14:56     9
3  2014-07-16 09:14:50     9
4  2014-07-16 09:14:49     9
5  2014-07-16 09:14:46     9
6  2014-07-16 09:14:46     9
7  2014-07-16 09:14:46     9
8  2014-07-16 09:14:46     9
9  2014-07-16 09:14:46     9
10 2014-07-16 09:14:46     9
11 2014-07-16 09:14:46     9
12 2014-07-15 14:41:56    14
13 2014-07-15 14:41:47    14
14 2014-07-15 14:41:30    14
15 2014-07-15 14:39:58    14
16 2014-07-15 14:39:57    14
17 2014-07-15 14:39:54    14
18 2014-07-15 14:39:53    14
19 2014-07-15 14:39:49    14

Upvotes: 2

Related Questions