python - 生成带日期的随机时间序列数据

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

我正在尝试生成带有日期的随机数据(整数),以便我可以在其上练习 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

有没有随机数据帧生成器可以生成这样的数据,每个日期间隔一个月?

最佳答案

您可以使用 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

关于python - 生成带日期的随机时间序列数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56310849/

相关文章:

python - print 和 pexpect 记录并读取文件

python seaborn : alpha by hue

python - Python 中的全局名称

python - 匹配两个DataFrame的更快方法

python - 如何将现有 Line2D 对象的样式复制到 plot() 调用? (matplotlib)

python - 尝试 fsync 目录时出错

python - Django 模型中可用的隐式字段有哪些?

python - 如何批量获取/放置 Yahoo 联系人

python - 一个数据点上的 Pandas 箱线图错误

python - 使每个结果存储在不同的数据框中?