Pandas cumulative function of series with dates and NaT

user1507844

This may be a known limitation, but I'm struggling to calculate the cumulative minimum of a series in Pandas when that series contains NaT's. Is there a way to make this work?

Simple example below:

import pandas as pd

s = pd.Series(pd.date_range('2008-09-15', periods=10, freq='m'))
s.loc[10] = pd.NaT
s.cummin()

ValueError: Could not convert object to NumPy datetime
unutbu

This bug has been fixed in Pandas 0.15.2 (to be released).


As a workaround, you could use skipna=False, and handle the NaTs "manually":

import pandas as pd
import numpy as np
np.random.seed(1)

s = pd.Series(pd.date_range('2008-09-15', periods=10, freq='m'))
s.loc[10] = pd.NaT
np.random.shuffle(s)
print(s)
# 0    2008-11-30
# 1    2008-12-31
# 2    2009-01-31
# 3    2009-06-30
# 4    2008-10-31
# 5    2009-03-31
# 6    2008-09-30
# 7    2009-04-30
# 8           NaT
# 9    2009-05-31
# 10   2009-02-28
# dtype: datetime64[ns]

mask = pd.isnull(s)
result = s.cummin(skipna=False)
result.loc[mask] = pd.NaT
print(result)

yields

0    2008-11-30
1    2008-11-30
2    2008-11-30
3    2008-11-30
4    2008-10-31
5    2008-10-31
6    2008-09-30
7    2008-09-30
8           NaT
9    2008-09-30
10   2008-09-30
dtype: datetime64[ns]

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Apply function on cumulative values of pandas series

From Dev

Pandas cumulative conditional sum by dates

From Dev

Apply Customize Cumulative Function to Pandas

From Dev

Padd missing dates in Pandas series

From Dev

Cumulative Sum Function on Pandas Data Frame

From Dev

Apply custom cumulative function to pandas dataframe

From Dev

Calculating a cumulative deviation from mean monthly value in pandas series

From Dev

Replace NAT dates with data from another column Python Pandas

From Dev

Replace NAT dates with data from another column Python Pandas

From Dev

Change values in pandas Series between dates

From Dev

Function with conditional statement to pandas series

From Dev

Applying strptime function to pandas series

From Dev

Pandas series operator as function parameter

From Dev

Does pandas/scipy/numpy provide a cumulative standard deviation function?

From Dev

Pandas time subset time series - dates ABOVE certain time

From Dev

How to combine dates and hours column into one index column in a pandas series?

From Dev

ValueError: Series lengths must match to compare when matching dates in Pandas

From Dev

Time-series x-axis dates from datetimeindex pandas

From Dev

How to complete time series data with some missing dates with pandas

From Dev

How to replace NaTs in a date column with dates in a pandas series?

From Dev

Find out the values and dates from series of data in pandas frame

From Dev

Time-series x-axis dates from datetimeindex pandas

From Dev

Adding Dates (Series) column from one DataFrame to the other Pandas, Python

From Dev

How to properly declare 'NaT' in a python function to be applied on a pandas dataframe?

From Dev

looping into dates and apply function to pandas dataframe

From Dev

Iterating through rows of a Pandas series within a function

From Dev

what's the inverse of the quantile function on a pandas Series?

From Dev

Pandas function for generating series from dataframe

From Dev

Faster alternative to Series.add function in pandas

Related Related

  1. 1

    Apply function on cumulative values of pandas series

  2. 2

    Pandas cumulative conditional sum by dates

  3. 3

    Apply Customize Cumulative Function to Pandas

  4. 4

    Padd missing dates in Pandas series

  5. 5

    Cumulative Sum Function on Pandas Data Frame

  6. 6

    Apply custom cumulative function to pandas dataframe

  7. 7

    Calculating a cumulative deviation from mean monthly value in pandas series

  8. 8

    Replace NAT dates with data from another column Python Pandas

  9. 9

    Replace NAT dates with data from another column Python Pandas

  10. 10

    Change values in pandas Series between dates

  11. 11

    Function with conditional statement to pandas series

  12. 12

    Applying strptime function to pandas series

  13. 13

    Pandas series operator as function parameter

  14. 14

    Does pandas/scipy/numpy provide a cumulative standard deviation function?

  15. 15

    Pandas time subset time series - dates ABOVE certain time

  16. 16

    How to combine dates and hours column into one index column in a pandas series?

  17. 17

    ValueError: Series lengths must match to compare when matching dates in Pandas

  18. 18

    Time-series x-axis dates from datetimeindex pandas

  19. 19

    How to complete time series data with some missing dates with pandas

  20. 20

    How to replace NaTs in a date column with dates in a pandas series?

  21. 21

    Find out the values and dates from series of data in pandas frame

  22. 22

    Time-series x-axis dates from datetimeindex pandas

  23. 23

    Adding Dates (Series) column from one DataFrame to the other Pandas, Python

  24. 24

    How to properly declare 'NaT' in a python function to be applied on a pandas dataframe?

  25. 25

    looping into dates and apply function to pandas dataframe

  26. 26

    Iterating through rows of a Pandas series within a function

  27. 27

    what's the inverse of the quantile function on a pandas Series?

  28. 28

    Pandas function for generating series from dataframe

  29. 29

    Faster alternative to Series.add function in pandas

HotTag

Archive