Change values in pandas Series between dates

s5s

I have a pandas Series() which has daily entries all set to False:

d = pd.Series(False, pd.bdate_range("20100101", periods=100, freq="D"))

I now want to set the values between the 15th and 20th of each month to True. I generate a Series with indices being the start/end dates and values of True:

s = pd.Series(True, pd.bdate_range("20100101", periods=100, freq="MS") + pd.DateOffset(14))
e = pd.Series(True, pd.bdate_range("20100101", periods=100, freq="MS") + pd.DateOffset(19))

At this point s and e will contain the start and end dates between which I want to set the values in d to True. I am not sure how to apply this to d elegantly.

A complication to this problem is if s and e are random (i.e. it is not always the 15-20 day of the month):

import random
sd = random.choice([1,2,3,4])
ed = random.choice([1,2,3,4]) + sd
del sd
del ed

# The delay is still constant for the entire series and not random per row
s = pd.Series(True, pd.bdate_range("20100101", periods=100, freq="MS") + pd.DateOffset(14 + sd))
e = pd.Series(True, pd.bdate_range("20100101", periods=100, freq="MS") + pd.DateOffset(19 + ed))
Stefan

Using your existing pd.Series, you can use boolean indexing via the .day attribute of the DateTimeIndex, or use np.in1d to the same effect:

d[(d.index.day >= 15) & (d.index.day <= 20)] = True

d[np.in1d(d.index.day, np.arange(15, 20))] = True

both resulting in:

2010-01-01    False
2010-01-02    False
2010-01-03    False
2010-01-04    False
2010-01-05    False
2010-01-06    False
2010-01-07    False
2010-01-08    False
2010-01-09    False
2010-01-10    False
2010-01-11    False
2010-01-12    False
2010-01-13    False
2010-01-14    False
2010-01-15     True
2010-01-16     True
2010-01-17     True
2010-01-18     True
2010-01-19     True
2010-01-20     True
2010-01-21    False
2010-01-22    False
2010-01-23    False
2010-01-24    False
2010-01-25    False
2010-01-26    False
2010-01-27    False
2010-01-28    False
2010-01-29    False
2010-01-30    False
              ...  
2010-03-12    False
2010-03-13    False
2010-03-14    False
2010-03-15     True
2010-03-16     True
2010-03-17     True
2010-03-18     True
2010-03-19     True
2010-03-20     True
2010-03-21    False
2010-03-22    False
2010-03-23    False
2010-03-24    False
2010-03-25    False
2010-03-26    False
2010-03-27    False
2010-03-28    False
2010-03-29    False
2010-03-30    False
2010-03-31    False
2010-04-01    False
2010-04-02    False
2010-04-03    False
2010-04-04    False
2010-04-05    False
2010-04-06    False
2010-04-07    False
2010-04-08    False
2010-04-09    False
2010-04-10    False
Freq: D, dtype: bool

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

pandas series filtering between values

From Dev

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

From Dev

Pandas, Filling between dates with average change between previous rows

From Dev

Pandas, Filling between dates with average change between previous rows

From Dev

Pandas dataframe reduce row values between specific dates

From Dev

Python Pandas Sum Values in Columns If date between 2 dates

From Dev

Padd missing dates in Pandas series

From Dev

Pandas Time Series: How to plot only times of day (no dates) against other values?

From Dev

Pandas Time Series: How to plot only times of day (no dates) against other values?

From Dev

Pandas: merging Series values

From Dev

Iterating Through Rows of Pandas DataFrame - change series value based on values in another series?

From Dev

Dynamically Change Dates for an In Between Event

From Dev

Selecting values between 2 dates

From Dev

How to query between a series of dates per year?

From Dev

Plot values of two time series with different dates

From Dev

Difference between dates in Pandas dataframe

From Dev

Pandas cumulative function of series with dates and NaT

From Dev

Finding the percent change of values in a Series

From Dev

select pandas dataframe between two given dates where values from two columns are equal

From Dev

pandas series: change order of index

From Dev

Pandas group by values in list (in series)

From Dev

Pandas: Printing the Names and Values in a Series

From Dev

Replacing many values in pandas Series

From Dev

Count specific values in a pandas series

From Dev

Grouping Period series values in Pandas

From Dev

How to change specific cell values in a pandas dataframe column series based on multiple conditions?

From Dev

dates change between client and server side (GWT)

From Dev

How to add values between two given dates

From Dev

Oracle db get values between specific dates

Related Related

  1. 1

    pandas series filtering between values

  2. 2

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

  3. 3

    Pandas, Filling between dates with average change between previous rows

  4. 4

    Pandas, Filling between dates with average change between previous rows

  5. 5

    Pandas dataframe reduce row values between specific dates

  6. 6

    Python Pandas Sum Values in Columns If date between 2 dates

  7. 7

    Padd missing dates in Pandas series

  8. 8

    Pandas Time Series: How to plot only times of day (no dates) against other values?

  9. 9

    Pandas Time Series: How to plot only times of day (no dates) against other values?

  10. 10

    Pandas: merging Series values

  11. 11

    Iterating Through Rows of Pandas DataFrame - change series value based on values in another series?

  12. 12

    Dynamically Change Dates for an In Between Event

  13. 13

    Selecting values between 2 dates

  14. 14

    How to query between a series of dates per year?

  15. 15

    Plot values of two time series with different dates

  16. 16

    Difference between dates in Pandas dataframe

  17. 17

    Pandas cumulative function of series with dates and NaT

  18. 18

    Finding the percent change of values in a Series

  19. 19

    select pandas dataframe between two given dates where values from two columns are equal

  20. 20

    pandas series: change order of index

  21. 21

    Pandas group by values in list (in series)

  22. 22

    Pandas: Printing the Names and Values in a Series

  23. 23

    Replacing many values in pandas Series

  24. 24

    Count specific values in a pandas series

  25. 25

    Grouping Period series values in Pandas

  26. 26

    How to change specific cell values in a pandas dataframe column series based on multiple conditions?

  27. 27

    dates change between client and server side (GWT)

  28. 28

    How to add values between two given dates

  29. 29

    Oracle db get values between specific dates

HotTag

Archive