Reading files with datetime type data
pd.read_csv(path, parse_dates=['columnname'],infer_datetime_format=True,date_parser=parser_func)
Does not work when NaN values are included.
Example of a parser function:
parser = lambda x: datetime.strptime(x,'%d %b %Y %I:%M:%S.%f')
Remember to import datetime from datetime
, datetime class inside datetime module
Workaround when NaN values exist:
df = pd.read_csv(file)
df.dropna(axis=0, how='any',inplace=True)
df.datestamp = pd.to_datetime(df.datestamp,format='%d %b %Y %I:%M:%S.%f')
By using df.datestamp.dt
, you can access attributes of the Datetime class.
网友评论