Home > Mobile >  Date Time Format Issues Python
Date Time Format Issues Python

Time:11-05

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.

CodePudding user response:

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
  • Related