在python中,如何用两个日期之间的随机日期替换数据框的日期列

在python中,如何用两个日期之间的随机日期替换数据框的日期列,python,Python,请帮助我用两个日期(2020-10-01和2020-10-31)之间的随机日期替换数据框的日期列。以下是供您参考的样本数据: stocks = pd.DataFrame({ 'ticker':np.repeat( ['aapl','goog','yhoo','msft'], 25 ), 'date':np.tile( pd.date_range('1/1/2011', periods=25, freq='D'), 4 ), 'price':(np.random.rand

请帮助我用两个日期(2020-10-01和2020-10-31)之间的随机日期替换数据框的日期列。以下是供您参考的样本数据:

stocks = pd.DataFrame({ 
    'ticker':np.repeat( ['aapl','goog','yhoo','msft'], 25 ),
    'date':np.tile( pd.date_range('1/1/2011', periods=25, freq='D'), 4 ),
    'price':(np.random.randn(100).cumsum() + 10) })
我已经尝试了下面的代码。但是,我无法在数据帧级别实现代码

import time
import datetime
import numpy as np

n_rows = 30

start_time = "01/11/2020"
end_time = "30/11/2020"

date2int = lambda s: time.mktime(datetime.datetime.strptime(s,"%d/%m/%Y").timetuple())
int2date = lambda s: datetime.datetime.fromtimestamp(s).strftime('%Y-%m-%d %H:%M:%S')

start_time = date2int(start_time)
end_time = date2int(end_time)

random_ints = np.random.randint(low=start_time, high=end_time, size=(n_rows,1))
random_dates = np.apply_along_axis(int2date, 1, random_ints).reshape(n_rows,1)

print (random_dates)

以下是获取随机日期的代码:

from datetime import datetime
import random

def random_date(first_date, second_date):
    first_timestamp = int(first_date.timestamp())
    second_timestamp = int(second_date.timestamp())
    random_timestamp = random.randint(first_timestamp, second_timestamp)
    return datetime.fromtimestamp(random_timestamp)

d1 = datetime.strptime("01/11/2020", "%d/%m/%Y")
d2 = datetime.strptime("30/11/2020", "%d/%m/%Y")

print (random_date(d1, d2))
2020-11-05 01:16:31
这将为您提供随机日期:

from datetime import datetime
import random

def random_date(first_date, second_date):
    first_timestamp = int(first_date.timestamp())
    second_timestamp = int(second_date.timestamp())
    random_timestamp = random.randint(first_timestamp, second_timestamp)
    return datetime.fromtimestamp(random_timestamp)

d1 = datetime.strptime("01/11/2020", "%d/%m/%Y")
d2 = datetime.strptime("30/11/2020", "%d/%m/%Y")

print (random_date(d1, d2))
2020-11-05 01:16:31
您是否正在尝试创建具有n个日期的数据帧

如果是,那么这将帮助您在
股票
数据框中创建随机日期

stocks = pd.DataFrame({ 
    'ticker':np.repeat( ['aapl','goog','yhoo','msft'], 25 )
    ,'date':[random_date(d1, d2) for _ in range(100)]
    ,'price':(np.random.randn(100).cumsum() + 10) 
    })
    
print (stocks)
输出将是:

ticker                date      price
0    aapl 2020-11-19 09:55:04  10.237543
1    aapl 2020-11-25 13:43:08  11.114540
2    aapl 2020-11-24 07:37:50  11.545754
3    aapl 2020-11-18 16:37:05  11.922486
4    aapl 2020-11-04 15:13:12  11.939903
..    ...                 ...        ...
95   msft 2020-11-27 19:14:59  13.909287
96   msft 2020-11-01 11:25:28  13.636019
97   msft 2020-11-03 14:29:13  13.620961
98   msft 2020-11-22 22:37:58  14.943541
99   msft 2020-11-10 19:31:35  15.180015

[100 rows x 3 columns]

您可以使用pd.Timestamp和random.randint在给定日期之间生成随机日期,并使用pd.to_datetime()将其转换为日期

start='2020-10-01'#指定开始日期
结束='2020-10-31'
n=len(股票)
x=np.random.randint(pd.Timestamp(start).value,pd.Timestamp(end).value,n,dtype=np.int64)
股票['date']=[pd.to_datetime((i/10**9)/(60*60)/24,单位为D')。对于i in x,strftime(“%Y-%m-%D”)]

ValueError:int32I的上限已超出范围。我已更新答案,是否仍有问题?目标是替换现有的日期列并应用随机日期。假设两个日期之间(开始日期='2020-10-01'和结束日期='2020-10-31')