首页 > 解决方案 > 将日期列表作为字符串转换为 PANDAS DatetimeIndex

问题描述

我有一个熊猫系列的日期(作为字符串)和频率作为整数:

date    
2020-02-02  24
2020-02-03  43
2020-02-07  465
2020-02-08  123
2020-02-09  234

但我需要它们作为下面代码中事件的输出:

all_days = pd.date_range('1/01/2020', periods=300, freq='D')
days = np.random.choice(all_days, 200)
events = pd.Series(np.random.randn(len(days)), index=days)

我的意思是保留我的数据但更改类型以便我可以在另一段代码中使用它们。先感谢您。

标签: pythonpandasdatetimeindex

解决方案


谢谢大家帮助我。我确实喜欢下面的代码:

import numpy as np; np.random.seed(sum(map(ord, 'calmap')))
import pandas as pd
import calmap

all_days = pd.date_range('1/01/2020', periods=300, freq='D')
days = np.random.choice(all_days, 200)
date_index = pd.to_datetime(dates_count.index)
maximum = max(dates_count.values)
minimum = min (dates_count.values)
events = pd.Series(((dates_count.values-minimum)*2/(maximum - minimum))-1, index=date_index)

推荐阅读