Converting Excel cells to time

Viewed 28

I'm trying to convert a list of time strings((?)not sure, although type (list[1]) says str), to timedate (or any other alternative if something else is better).

I want to sum up these times if a certain condition is met. This is what I got so far:

import os
import csv
import datetime
import pandas as pd

read_file = pd.read_excel ('test.xlsx')
read_file2 = read_file['Duration']

uren_lijst = []

for row in read_file2:
    for row in read_file2:
    uren_lijst.append(row)

I already tried using the 'pd.to_datetime' method, but this seems to create timestamps and I only neet the hours and I am not interested in the date itself.

1 Answers

You can try this:

First convert it to a datetime column. Then extract hour from it.

read_file['Duration'] = pd.to_datetime(read_file['Duration'])
hours = read_file['Duration'].dt.hour

After OP's comment:

time = read_file['Duration'].dt.time
Related