date_range返回list
时间: 2023-07-30 08:09:14 浏览: 67
如果您需要将`date_range`返回的时间序列对象转换为Python内置的列表对象,可以通过调用`tolist()`方法来实现。具体代码如下所示:
```python
import pandas as pd
# 生成时间序列对象
date_rng = pd.date_range(start='1/1/2021', end='1/10/2021', freq='D')
# 将时间序列对象转换为列表对象
date_list = date_rng.tolist()
# 打印列表对象
print(date_list)
```
上述代码将生成一个包含指定时间范围内的所有日期时间点的时间序列对象,并将其转换为包含这些日期时间点的Python列表对象,最后打印输出。
相关问题
python date_range倒序
要倒序生成日期序列,可以使用pandas的date_range函数的参数start和end,并将end设置为较早的日期,将start设置为较晚的日期。这样就可以生成一个倒序的日期序列。例如,使用date_range函数生成一个从2022年1月1日到2022年1月10日的倒序日期序列的代码如下:
```python
import pandas as pd
dates = pd.date_range(end='2022-01-10', start='2022-01-01')
```
这样就会生成一个包含从2022年1月10日到2022年1月1日的日期序列。
#### 引用[.reference_title]
- *1* *3* [pandas常用函数之date_range](https://blog.csdn.net/You_are_my_dream/article/details/70209757)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item]
- *2* [python(numpy,pandas6)——pandas数据形式的简介,排序方式sort_index,sort_values](https://blog.csdn.net/qq_38800089/article/details/113835202)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item]
[ .reference_list ]
def data_processing(data): # 日期缺失,补充 data.fillna(method='ffill', inplace=True) date_history = pd.DataFrame(data.iloc[:, 0]) data_history = pd.DataFrame(data.iloc[:, 1]) date_history = np.array(date_history) data_history = [x for item in np.array(data_history).tolist() for x in item] # 缺失值处理 history_time_list = [] for date in date_history: date_obj = datetime.datetime.strptime(date[0], '%Y/%m/%d %H:%M') #将字符串转为 datetime 对象 history_time_list.append(date_obj) start_time = history_time_list[0] # 起始时间 end_time = history_time_list[-1] # 结束时间 delta = datetime.timedelta(minutes=15) #时间间隔为15分钟 time_new_list = [] current_time = start_time while current_time <= end_time: time_new_list.append(current_time) current_time += delta # 缺失位置记录 code_list = [] for i in range(len(time_new_list)): code_list = code_list history_time_list = history_time_list while (time_new_list[i] - history_time_list[i]) != datetime.timedelta(minutes=0): history_time_list.insert(i, time_new_list[i]) code_list.append(i) for i in code_list: data_history.insert(i, data_history[i - 1]) # 输出补充好之后的数据 data = pd.DataFrame({'date': time_new_list, 'load': data_history}) return data 优化代码
可以尝试将循环语句进行向量化,使用 pandas 库提供的数据处理函数。具体来说,可以将以下部分进行优化:
```
history_time_list = []
for date in date_history:
date_obj = datetime.datetime.strptime(date[0], '%Y/%m/%d %H:%M')
history_time_list.append(date_obj)
```
可以改写为:
```
history_time_list = pd.to_datetime(date_history.iloc[:, 0], format='%Y/%m/%d %H:%M')
```
同样地,可以将以下部分进行优化:
```
time_new_list = []
current_time = start_time
while current_time <= end_time:
time_new_list.append(current_time)
current_time += delta
```
可以改写为:
```
time_new_list = pd.date_range(start=start_time, end=end_time, freq='15min')
```
最后,可以使用 pandas 库提供的函数 `fillna()` 进行缺失值处理,而无需使用循环语句。
优化后的代码如下:
```
def data_processing(data):
# 日期缺失,补充
data.fillna(method='ffill', inplace=True)
# 转换时间格式
date_history = pd.to_datetime(data.iloc[:, 0], format='%Y/%m/%d %H:%M')
# 缺失值处理
start_time = date_history.iloc[0]
end_time = date_history.iloc[-1]
time_new_list = pd.date_range(start=start_time, end=end_time, freq='15min')
data_history = data.iloc[:, 1].fillna(method='ffill')
data_history = data_history.reindex(code_list, method='ffill')
# 输出补充好之后的数据
data = pd.DataFrame({'date': time_new_list, 'load': data_history})
return data
```
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![7z](https://img-home.csdnimg.cn/images/20210720083312.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)