create dataframe with date

Solutions on MaxInterview for create dataframe with date by the best coders in the world

showing results for - "create dataframe with date"
Jonathan
04 Feb 2020
1import pandas as pd
2import numpy as np
3
4np.random.seed(0)
5# create an array of 5 dates starting at '2015-02-24', one per minute
6rng = pd.date_range('2015-02-24', periods=5, freq='T')
7df = pd.DataFrame({ 'Date': rng, 'Val': np.random.randn(len(rng)) }) 
8
9print (df)
10# Output:
11#                  Date       Val
12# 0 2015-02-24 00:00:00  1.764052
13# 1 2015-02-24 00:01:00  0.400157
14# 2 2015-02-24 00:02:00  0.978738
15# 3 2015-02-24 00:03:00  2.240893
16# 4 2015-02-24 00:04:00  1.867558
17
18# create an array of 5 dates starting at '2015-02-24', one per day
19rng = pd.date_range('2015-02-24', periods=5, freq='D')
20df = pd.DataFrame({ 'Date': rng, 'Val' : np.random.randn(len(rng))}) 
21
22print (df)
23# Output:
24#         Date       Val
25# 0 2015-02-24 -0.977278
26# 1 2015-02-25  0.950088
27# 2 2015-02-26 -0.151357
28# 3 2015-02-27 -0.103219
29# 4 2015-02-28  0.410599
30
31# create an array of 5 dates starting at '2015-02-24', one every 3 years
32rng = pd.date_range('2015-02-24', periods=5, freq='3A')
33df = pd.DataFrame({ 'Date': rng, 'Val' : np.random.randn(len(rng))})  
34
35print (df)
36# Output:
37#         Date       Val
38# 0 2015-12-31  0.144044
39# 1 2018-12-31  1.454274
40# 2 2021-12-31  0.761038
41# 3 2024-12-31  0.121675
42# 4 2027-12-31  0.443863
43