【问题标题】:Generate random timeseries data with dates生成带有日期的随机时间序列数据
【发布时间】:2019-10-12 03:08:58
【问题描述】:

我正在尝试生成带有日期的随机数据(整数),以便我可以在其上练习 pandas 数据分析命令并绘制时间序列图。

             temp     depth   acceleration
2019-01-1 -0.218062 -1.215978 -1.674843
2019-02-1 -0.465085 -0.188715  0.241956
2019-03-1 -1.464794 -1.354594  0.635196
2019-04-1  0.103813  0.194349 -0.450041
2019-05-1  0.437921  0.073829  1.346550

是否有任何随机数据帧生成器可以生成这样的内容,每个日期都有一个月的间隔?

【问题讨论】:

  • 你检查this了吗?
  • 我尝试只使用 pandas 模块。

标签: python pandas python-2.7 machine-learning time-series


【解决方案1】:

numpy.random.randnumpy.random.randint 函数与DataFrame 构造函数一起使用:

np.random.seed(2019)
N = 10
rng = pd.date_range('2019-01-01', freq='MS', periods=N)
df = pd.DataFrame(np.random.rand(N, 3), columns=['temp','depth','acceleration'], index=rng)

print (df)
                temp     depth  acceleration
2019-01-01  0.903482  0.393081      0.623970
2019-02-01  0.637877  0.880499      0.299172
2019-03-01  0.702198  0.903206      0.881382
2019-04-01  0.405750  0.452447      0.267070
2019-05-01  0.162865  0.889215      0.148476
2019-06-01  0.984723  0.032361      0.515351
2019-07-01  0.201129  0.886011      0.513620
2019-08-01  0.578302  0.299283      0.837197
2019-09-01  0.526650  0.104844      0.278129
2019-10-01  0.046595  0.509076      0.472426

如果需要整数:

np.random.seed(2019)
N = 10
rng = pd.date_range('2019-01-01', freq='MS', periods=N)
df = pd.DataFrame(np.random.randint(20, size=(10, 3)), 
                  columns=['temp','depth','acceleration'], 
                  index=rng)

print (df)
            temp  depth  acceleration
2019-01-01     8     18             5
2019-02-01    15     12            10
2019-03-01    16     16             7
2019-04-01     5     19            12
2019-05-01    16     18             5
2019-06-01    16     15             1
2019-07-01    14     12            10
2019-08-01     0     11            18
2019-09-01    15     19             1
2019-10-01     3     16            18

【讨论】:

  • 每次运行时都会得到不同的输出。哪里不对了?我可以做一个声明吗?
  • @NodeSlayer - 之前使用np.random.seed(1000),检查this
【解决方案2】:

您可以使用 pandas.util.testing

import pandas.util.testing as testing
import numpy as np
np.random.seed(1)

testing.N, testing.K = 5, 3  # Setting the rows and columns of the desired data

print testing.makeTimeDataFrame(freq='MS')
>>>
                   A         B         C
2000-01-01 -0.488392  0.429949 -0.723245
2000-02-01  1.247192 -0.513568 -0.512677
2000-03-01  0.293828  0.284909  1.190453
2000-04-01 -0.326079 -1.274735 -0.008266
2000-05-01 -0.001980  0.745803  1.519243

或者,如果您需要对生成的随机值进行更多控制,您可以使用类似

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

rows,cols = 5,3
data = np.random.rand(rows,cols) # You can use other random functions to generate values with constraints
tidx = pd.date_range('2019-01-01', periods=rows, freq='MS') # freq='MS'set the frequency of date in months and start from day 1. You can use 'T' for minutes and so on
data_frame = pd.DataFrame(data, columns=['a','b','c'], index=tidx)
print data_frame
>>>
                   a         b         c
2019-01-01  0.992856  0.217750  0.538663
2019-02-01  0.189226  0.847022  0.156730
2019-03-01  0.572417  0.722094  0.868219
2019-04-01  0.023791  0.653147  0.857148
2019-05-01  0.729236  0.076817  0.743955

【讨论】:

  • 谢谢,第一个选项很适合我的练习。但是每次我运行时都会得到不同的输出。我错过了什么?
  • 在导入模块后立即使用np.random.seed(1)。如果您在生成随机数据之前设置种子值,它将在每次运行中生成相同的数据
  • @fireblaze - 使用 MS 表示月份的开始
  • @jezrael 已更新。
  • pandas.util.testing 将被弃用
猜你喜欢
  • 2013-02-05
  • 1970-01-01
  • 1970-01-01
  • 2021-08-30
  • 2019-06-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多