I have a field in a pandas dataframe which I want to truncate the minute from, ie cast from time to string, split it and retain the hour only.
The time field in my dataframe is of type object and what I have tried to do is as below (accidents is my pandas dataframe by the way):
for row in range (1, len(accidents)):
hour = row['Time'].strftime("%H").split(":", 1)[0]
For example, what I want to do is to change 17:45 to 17.
Image of accidents Dataframe is as below.
The datatype for the Time object is as below.
When I run the above code, I get an error "TypeError: 'int' object is not subscriptable"
I know this is a casting error of some form, but I don't know how to fix it. (I am pretty new to Python).
The csv file can be re-produced (truncated here to include the relevant field only) if requested. Accident_Index,Location_Easting_OSGR,Location_Northing_OSGR,Longitude,Latitude,Police_Force,Accident_Severity,Number_of_Vehicles,Number_of_Casualties,Date,Day_of_Week,Time, 200501BS00001,525680,178240,-0.191170,51.489096,1,2,1,1,04/01/2005,3,17:42
The datetime
module's strftime()
needs to have a format argument (see proper usage below).
>>> datetime.now().strftime("%m/%d/%Y, %H:%M:%S")
'08/09/2020, 02:48:25'
(A good strftime()
cheat-sheet is available here )
In your case, it looks like you're just trying to get the hour number. For this, you should use the following format string to just get the hour: "%H"
. If you need to get this as an integer (if you need to do math with it), you can simply surround it by int()
. If you're just going to display this number, you can leave it as a string. strftime()
returns a string by default.
Try this code:
hour = int(row['Time'].strftime("%H"))
You can use the hour
attribute of the Time column
d = [d for d in pd.date_range(dt.datetime(2020,5,1,2),
dt.datetime(2020,5,3,4), freq="45min")
] # miss some sample times...
# random manipulation of rawIdx so there are some rows where ts is not in rawIdx
df = pd.DataFrame({"Time":d,
"val":[random.randint(0,50) for x in d]})
df["Hour"] = df["Time"].dt.hour
print(df.head().to_string(index=False))
output
Time val Hour
2020-05-01 02:00:00 39 2
2020-05-01 02:45:00 23 2
2020-05-01 03:30:00 9 3
2020-05-01 04:15:00 25 4
2020-05-01 05:00:00 41 5
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.