Find centralized, trusted content and collaborate around the technologies you use most. Connect and share knowledge within a single location that is structured and easy to search. I have dataframes with DateTime indices of various types could be weekly, monthly, annual data.
I want to generate columns that are lagged values of other columns. I get these imported from a spreadsheet, I'm not generating the datetime index inside python. I'm struggling to find the 'pythonic' way of doing this.
I figure if I use Pandas' datetime capability, the lagging might be more robust in the case of weird or exceptional data. The toy example which correctly works makes a new column that has the 'foo' value of the previous month. Seems like I'm missing some subtlety of Pandas datetime indices I think.
Plus I'm not even sure this is the ideal way to do this. There is another problem - many NaT in index in toy DataFrameso index has duplicates values. Maybe some datetime are duplicated too. If need remove all records with NaN NaT in indexuse notnull with boolean indexing :.
Stack Overflow for Teams — Start collaborating and sharing organizational knowledge. Create a free Team Why Teams? Learn more about Collectives. Learn more about Teams. Pythonic way to lag datetime-indexed columns Ask Question. Asked 7 years, 2 months ago.
Modified 7 years, 2 months ago. Viewed 2k times. I made a toy example that seems to work, but it fails on my real-world example. DataFrame pd. Series np. Try using. Improve this question. Add a comment. Sorted by: Reset to default.