Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about Collectives
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
I am trying to run a for loop and grab a bunch of files however the dates for those files are in a bit of a weird format. That looks like
("%Y-%m-%d_%H:%M")
. When I pass through
("%Y-%m-%d %H:%M")
the loop works fine. However for the former I am getting a value error could not convert string to timestamp.
Here is my shortened code
beginning_time = datetime(year,month,day,hour,minute)
beginning_time_str = beginning_time.strftime("%Y-%m-%d_%H:%M")
end_time = datetime(year,month,day_end,hour_end,minute)
end_time_str = end_time.strftime("%Y-%m-%d_%H:%M")
timerange_HRRR =
pd.date_range(start=beginning_time_str,end=end_time_str,freq='1H')
timerange_HRRR = timerange_HRRR.astype(str)
grib_file_2d_base = f'{wrf_cases_dir}
{wrfoutdir}/wrf2d_{domain}_'+timerange_HRRR+'.grib2'
for fpath in grib_file_2d_base:
print(fpath)
Here is a shortened version of the traceback I'm recieving
ValueError Traceback (most recent
call last)
Input In [23], in <cell line: 31>()
30 timerange =
pd.date_range(start=starttime,end=endtime,freq='5min')
---> 31 timerange_HRRR =
pd.date_range(start=beginning_time_str,end=end_time_str,freq='1H')
32 timerange_HRRR = timerange_HRRR.astype(str)
35 base_dir = '/network/rit/lab/minderlab_rit/NYSM'
ValueError: could not convert string to Timestamp
–
This is because the pd.date_range()
function can not interpret your datetime string in the given format with the underscore. You must either pass it a datetime object (beginning_time
, end_time
in your example) or a string in a standard datetime format (e.g. Y-m-d H:M:S
), see here.
If you really must use your custom datetime format, you have to create the Pandas date range first and format its entries into a string before writing them to a file.
For example:
beginning_time = datetime(year,month,day,hour,minute)
end_time = datetime(year,month,day_end,hour_end,minute)
timerange_HRRR = pd.date_range(start=beginning_time,end=end_time,freq='1H')
timerange_HRRR = timerange_HRRR.astype(str)
grib_file_2d_base = f'{wrf_cases_dir}{wrfoutdir}/wrf2d_{domain}_'+timerange_HRRR+'.grib2'
grib_file_2d_base = [s.replace(" ","_") for s in grib_file_2d_base]
for fpath in grib_file_2d_base:
print(fpath)
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.