Skip to content

Commit

Permalink
Replacing date_parser by pd.to_datetime for old file version
Browse files Browse the repository at this point in the history
This gave a deprecation warning in the pandas version currently used in pypromice (#127), but is now not supported anymore and is throwing an error in the pandas version I use locally.
  • Loading branch information
BaptisteVandecrux committed Aug 14, 2023
1 parent a8ac27d commit ddac8bf
Showing 1 changed file with 10 additions and 26 deletions.
36 changes: 10 additions & 26 deletions src/pypromice/process/aws.py
Original file line number Diff line number Diff line change
Expand Up @@ -333,11 +333,18 @@ def getL0(infile, nodata, cols, skiprows, file_version,
if file_version == 1:
df = pd.read_csv(infile, comment=comment, index_col=0,
na_values=nodata, names=cols,
parse_dates={'time': ['year', 'doy', 'hhmm']},
date_parser=_getDateParserV1, sep=delimiter,
sep=delimiter,
skiprows=skiprows, skip_blank_lines=True,
usecols=range(len(cols)),
low_memory=False)
df['time'] = pd.to_datetime(
df.year.astype(str) \
+ df.doy.astype(str).str.zfill(3) \
+ df.hhmm.astype(str).str.zfill(4),
format='%Y%j%H%M'
)
df = df.set_index('time')

else:
df = pd.read_csv(infile, comment=comment, index_col=0,
na_values=nodata, names=cols, parse_dates=True,
Expand Down Expand Up @@ -866,30 +873,7 @@ def _addAttr(ds, key, value):
# print(f'Unable to add metadata to {key.split(".")[0]}')
else:
ds.attrs[key] = value

def _getDateParserV1(y, doy, t): #TODO fix deprecation warning
'''Convert for yyyy,doy,hhmm (without leading 0s) to a pandas datetime.
Example: "2007,90,430" to "2007-03-31 04:30:00"
This may produce the following deprecation warning:
FutureWarning: Use pd.to_datetime instead.
Parameters
----------
y : int
Year
doy: int
Day of year
t : int
Hour
Returns
-------
pd.Datetime
Datetime object
'''
return pd.to_datetime(f'{y}-{str(doy).zfill(3)}:{str(t).zfill(4)}',
format='%Y-%j:%H%M')


#------------------------------------------------------------------------------

Expand Down

0 comments on commit ddac8bf

Please sign in to comment.