python - Construct DatetimeIndex if you only have year -
i have data of structure
country year pop 606 algeria 1966 12339.140 730 algeria 1968 13146.267 793 algeria 1969 13528.304 856 algeria 1970 13931.846 924 algeria 1971 14335.388
now want create first-differences per country based on year (difference per year). if weren't interval concern, i'd along lines of
df.sort(['country', 'year']).set_index(['country', 'year']).diff()
instead, guess have convert year
to_datetime()
first. there simple way create datetime column contains years only? , there different more natural approach create differences on time?
you do
df.set_index(df.year.map(lambda x: datetime.datetime(x, 1, 1)))
that uses concept of left-open intervals.
another possibility is
df.set_index(df.year.map(pd.period))
both return equally well-defined indexes, in latter case might output of df.diff()
better since states year.
Comments
Post a Comment