How to create a timeseries starting "2000-01-01" and 10 weekends(saturdays) after that having random numbers as values.(in Python)?
Question:
rnge = pd.date_range(start = "2000-01-01" ,periods =14 , freq = "D")
rnge
I want saturdays to be excluded.
Answers:
I’m not sure whether I understood the question (didn’t get the part about random numbers).
If you just want to filter out Saturdays then it can be done like this:
rnge = pd.date_range(start="2000-01-01", periods=14, freq="D")
rnge = rnge[rnge.weekday != 5]
Result:
DatetimeIndex(['2000-01-02', '2000-01-03', '2000-01-04', '2000-01-05',
'2000-01-06', '2000-01-07', '2000-01-09', '2000-01-10',
'2000-01-11', '2000-01-12', '2000-01-13', '2000-01-14'],
dtype='datetime64[ns]', freq=None)
dates = pd.date_range('2000-01-01', periods=10, freq='W-SAT')
# Create a list of random numbers
values = np.random.randint(0, 10, size=10)
df = pd.Series(values, index=dates)
print(df)
Output:
2000-01-01 7
2000-01-08 0
2000-01-15 8
2000-01-22 1
2000-01-29 5
2000-02-05 3
2000-02-12 8
2000-02-19 7
2000-02-26 8
2000-03-04 4
Freq: W-SAT, dtype: int64
‘W-SAT’ means every Saturday. You can check date_range function here.
pandas.date_range — pandas 1.5.2 documentation
rnge = pd.date_range(start = "2000-01-01" ,periods =14 , freq = "D")
rnge
I want saturdays to be excluded.
I’m not sure whether I understood the question (didn’t get the part about random numbers).
If you just want to filter out Saturdays then it can be done like this:
rnge = pd.date_range(start="2000-01-01", periods=14, freq="D")
rnge = rnge[rnge.weekday != 5]
Result:
DatetimeIndex(['2000-01-02', '2000-01-03', '2000-01-04', '2000-01-05',
'2000-01-06', '2000-01-07', '2000-01-09', '2000-01-10',
'2000-01-11', '2000-01-12', '2000-01-13', '2000-01-14'],
dtype='datetime64[ns]', freq=None)
dates = pd.date_range('2000-01-01', periods=10, freq='W-SAT')
# Create a list of random numbers
values = np.random.randint(0, 10, size=10)
df = pd.Series(values, index=dates)
print(df)
Output:
2000-01-01 7
2000-01-08 0
2000-01-15 8
2000-01-22 1
2000-01-29 5
2000-02-05 3
2000-02-12 8
2000-02-19 7
2000-02-26 8
2000-03-04 4
Freq: W-SAT, dtype: int64
‘W-SAT’ means every Saturday. You can check date_range function here.
pandas.date_range — pandas 1.5.2 documentation