python - How to find the number of the day in a year based on the actual dates using Pandas? -
my data frame data
has date variable dateopen
following format date_format = "%y-%m-%d %h:%m:%s.%f"
, have new column called openday
day number based on 365 days year. tried applying following
data['dateopen'] = [datetime.strptime(dt, date_format) dt in data['dateopen']] data['openday'] = [dt.day dt in data['dateopen']]
however, day in month. example if date 2013-02-21 10:12:14.3
above formula return 21. however, want return 52 31 days january plus 21 days february.
is there simple way in pandas?
on latest pandas can use date-time properties:
>>> ts = pd.series(pd.to_datetime(['2013-02-21 10:12:14.3'])) >>> ts 0 2013-02-21 10:12:14.300000 dtype: datetime64[ns] >>> ts.dt.dayofyear 0 52 dtype: int64
on older versions, may able convert datetimeindex
, use .dayofyear
property:
>>> pd.index(ts).dayofyear # may work array([52], dtype=int32)
Comments
Post a Comment