0

I am currently having issues with date-time format, particularly converting string input to the correct python datetime format

          Date/Time  Dry_Temp[C]  Wet_Temp[C]  Solar_Diffuse_Rate[[W/m2]]  \
0   01/01  00:10:00         8.45     8.237306                         0.0   
1   01/01  00:20:00         7.30     6.968360                         0.0   
2   01/01  00:30:00         6.15     5.710239                         0.0   
3   01/01  00:40:00         5.00     4.462898                         0.0   
4   01/01  00:50:00         3.85     3.226244                         0.0   

These are current examples of timestamps I have in my time, I have tried splitting date and time such that I now have the following columns:

   WC_Humidity[%]  WC_Htgsetp[C]  WC_Clgsetp[C]       Date      Time  
0       55.553640             18             26 1900-01-01  00:10:00  
1       54.204342             18             26 1900-01-01  00:20:00  
2       51.896272             18             26 1900-01-01  00:30:00  
3       49.007770             18             26 1900-01-01  00:40:00  
4       45.825810             18             26 1900-01-01  00:50:00  

I have managed to get the year into datetime format, but there are still 2 problems to resolve:

  • the data was not recorded in 1900, so I would like to change the year in the Date,
  • I get the following error whent rying to convert time into time datetime python format
pandas/_libs/tslibs/strptime.pyx in pandas._libs.tslibs.strptime.array_strptime()

ValueError: time data '00:00:00' does not match format ' %m/%d  %H:%M:%S' (match)

I tried having 24:00:00, however, python didn't like that either...

preferences: I would prefer if they were both in the same cell without having to split this information into two columns. I would also like to get rid of the seconds data as the data was recorded in 10 min intervals so there is no need for seconds in my case.

Any help would be greatly appreciated.

4
  • 1
    The error tells you what the problem is: you have elements like '00:00:00' in the input. That's not the format you specified. Commented Nov 5, 2021 at 11:16
  • Also, "so I would like to change the year in the Date" which date? The example you show doesn't have a year specified Commented Nov 5, 2021 at 11:18
  • I think I made a silly mistake as I have managed to get the time to work, However, it also associates my timeseries data with a date, and the year is still 1900, any ideas on how to change year? Commented Nov 5, 2021 at 11:19
  • 1900-01-01 the year in this case would be 1900, Commented Nov 5, 2021 at 11:20

1 Answer 1

1

the data was not recorded in 1900, so I would like to change the year in the Date,

datetime.datetime.replace method of datetime.datetime instance is used for this task consider following example:

import pandas as pd
df = pd.DataFrame({"when":pd.to_datetime(["1900-01-01","1900-02-02","1900-03-03"])})
df["when"] = df["when"].apply(lambda x:x.replace(year=2000))
print(df)

output

        when
0 2000-01-01
1 2000-02-02
2 2000-03-03

Note that it can be used also without pandas for example

import datetime
d = datetime.datetime.strptime("","")  # use all default values which result in midnight of Jan 1 of year 1900
print(d)  # 1900-01-01 00:00:00
d = d.replace(year=2000)
print(d)  # 2000-01-01 00:00:00
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you Daweo, Is there a way to have a cell that just displays the time? I have currently got the time in datetime format which displays the time, but also attaches a date which is unneccessary as i already have a date column. or a way to have both date and time read from my cells?
@LoaiAlnouri if you want to have strings with representation of hour-minutes you can use .strftime with %H:%M code i.e. df['hourmin'] = df['column'].dt.strftime("%H:%M") where column is holding datetimes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.