提问人:Audrey Allen 提问时间:11/16/2023 最后编辑:rioV8Audrey Allen 更新时间:11/16/2023 访问量:46
如何在 Python 中合成 72 小时内的日期时间数据?它必须是正态分布。另外如何将日期数组输出到 csv?
How to synthesize datetime data over a period of 72 hours in Python? It needs to be normal distribution. Also how to output the array of dates to csv?
问:
我花了几个星期来解决这个问题。我有以下代码,它们提取了日期时间值,但是当我导出到csv时,它只将一个值导出到csv。
import datetime
import random
import pandas as pd
from numpy import savetxt
from numpy import asarray
min_date = pd.to_datetime('2019-01-01 00:00:00')
print(min_date)
max_date = pd.to_datetime('2019-01-01 23:59:59')
print(max_date)
for x in range(100):
print(min_date + datetime.timedelta(seconds=random.randint(0, int((max_date - min_date).total_seconds())),))
Test = min_date + datetime.timedelta(seconds=random.randint(0, int((max_date - min_date).total_seconds())),)
print(Test)
上面的代码显示了以下正确的输出。
2019-01-01 00:00:00
2019-01-01 23:59:59
2019-01-01 20:39:37
2019-01-01 11:46:57
2019-01-01 17:17:34
2019-01-01 00:23:27
2019-01-01 21:35:03
2019-01-01 06:24:01
2019-01-01 05:13:12
2019-01-01 02:56:24
但是当我导出到 CSV 时(下面的代码)
EDAttendances = {'DateofAttendance1': (Test),
'Age': (df.Age),
}
df = pd.DataFrame(EDAttendances)
# Save the dataframe to a CSV file
df.to_csv('EDAttendances.csv', index=False)
df['DateofAttendance1']
plt.hist(df['DateofAttendance1'])
我在 csv 中多次只得到一个日期时间。
2019-01-01 01:56:14,58
2019-01-01 01:56:14,44
2019-01-01 01:56:14,82
2019-01-01 01:56:14,63
2019-01-01 01:56:14,84
2019-01-01 01:56:14,82
2019-01-01 01:56:14,81
2019-01-01 01:56:14,41
2019-01-01 01:56:14,21
2019-01-01 01:56:14,76
2019-01-01 01:56:14,52
2019-01-01 01:56:14,50
2019-01-01 01:56:14,38
答: 暂无答案
评论
Test
是单个变量,而不是数组/列表Normal
.....total_seconds()