Python可视化数据分析06、Pandas进阶
Python可视化数据分析06、Pandas进阶
📋前言📋
💝博客:【红目香薰的博客_CSDN博客-计算机理论,2022年蓝桥杯,MySQL领域博主】💝
✍本文由在下【红目香薰】原创,首发于CSDN✍
🤗2022年最大愿望:【服务百万技术人次】🤗
💝Python初始环境地址:【Python可视化数据分析01、python环境搭建】💝
环境需求
环境:win10
开发工具:PyCharm Community Edition 2021.2
数据库:MySQL5.6
目录
pip3 config set global.index-url https://repo.huaweicloud.com/repository/pypi/simple
pip3 config list
pip3 install --upgrade pip
pip3 install requests
pip3 install pandas
datetime对象
时间序列数据是一种重要的结构化数据形式。
在Python语言中,datetime模块中的datetime、time和calendar等类都可以用来存储时间类型及进行一些转换和运算操作
datetime对象的常用操作如下:
datetime对象间的减法运算会得到一个timedelta对象,timedelta对象代表两个时间之间的时间差
datetime对象与它所保存的字符串格式时间戳之间可以互相转换。
import datetime
n = datetime.datetime.now()
# str(time)函数返回字符串格式时间戳
print(str(n))
# time.strftime(format)函数返回以可读字符串表示的当地时间,格式由format决定
print(n.strftime("%Y-%m-%d"))
# time.strptime(string, format)函数根据format指定的格式,把一个时间字符串string解析为时间
print(datetime.datetime.strptime("2022-7-27 19:19:17", "%Y-%m-%d %H:%M:%S"))
时间序列
Pandas最基本的时间日期对象是一个从Series派生出来的子类TimeStamp。
Pandas最基本的时间序列类型就是以时间戳(TimeStamp)为index元素的Series类型。
时间序列只是index比较特殊的Series,因此一般的索引操作对时间序列依然有效。
import datetime as datetime
import pandas as pd
import numpy as np
from pandas import Series
print("当前时间:", pd.to_datetime(datetime.datetime.now()))
dates = [datetime.datetime(2022, 1, 1), datetime.datetime(2022, 1, 2), datetime.datetime(2022, 12, 31)]
ts = Series(np.random.rand(3), index=dates)
print("------------------")
print(ts)
# 普通索引操作
print("------------------")
print("下标:", ts.index)
print("------------------")
print("下标[2]:", ts.index[2])
print("------------------")
# 使用各种字符串进行索引
print(ts["20220101"])
print("--------20220101----------")
print(ts["2022-01-01"])
print("--------2022-01-01----------")
print(ts["2022"])
print("--------2022----------")
print(ts["2022-1-1":"2022-1-5"]) # 范围
print("--------2022-1-1 2022-1-5----------")
date_range()
参数值 |
说明 |
Y |
年 |
M |
月 |
D |
日 |
H |
小时 |
T |
分钟 |
S |
秒 |
import pandas as pd
print(pd.date_range("20220101", "20220110"))
print(pd.date_range(start="20220101", periods=10))
print(pd.date_range(end="20220110", periods=10))
print(pd.date_range("20220101", "20220601", freq="M"))
print(pd.date_range('20220918', '2022-09-28'))
print(pd.date_range('2022/09/18', '2022-09-28'))
print(pd.date_range('2022/9/18', '2022-9-19', freq="3H"))
DatetimeIndex(['2022-01-01', '2022-01-02', '2022-01-03', '2022-01-04',
'2022-01-05', '2022-01-06', '2022-01-07', '2022-01-08',
'2022-01-09', '2022-01-10'],
dtype='datetime64[ns]', freq='D')
DatetimeIndex(['2022-01-01', '2022-01-02', '2022-01-03', '2022-01-04',
'2022-01-05', '2022-01-06', '2022-01-07', '2022-01-08',
'2022-01-09', '2022-01-10'],
dtype='datetime64[ns]', freq='D')
DatetimeIndex(['2022-01-01', '2022-01-02', '2022-01-03', '2022-01-04',
'2022-01-05', '2022-01-06', '2022-01-07', '2022-01-08',
'2022-01-09', '2022-01-10'],
dtype='datetime64[ns]', freq='D')
DatetimeIndex(['2022-01-31', '2022-02-28', '2022-03-31', '2022-04-30',
'2022-05-31'],
dtype='datetime64[ns]', freq='M')
DatetimeIndex(['2022-09-18', '2022-09-19', '2022-09-20', '2022-09-21',
'2022-09-22', '2022-09-23', '2022-09-24', '2022-09-25',
'2022-09-26', '2022-09-27', '2022-09-28'],
dtype='datetime64[ns]', freq='D')
DatetimeIndex(['2022-09-18', '2022-09-19', '2022-09-20', '2022-09-21',
'2022-09-22', '2022-09-23', '2022-09-24', '2022-09-25',
'2022-09-26', '2022-09-27', '2022-09-28'],
dtype='datetime64[ns]', freq='D')
DatetimeIndex(['2022-09-18 00:00:00', '2022-09-18 03:00:00',
'2022-09-18 06:00:00', '2022-09-18 09:00:00',
'2022-09-18 12:00:00', '2022-09-18 15:00:00',
'2022-09-18 18:00:00', '2022-09-18 21:00:00',
'2022-09-19 00:00:00'],
dtype='datetime64[ns]', freq='3H')Process finished with exit code 0