gpt4 book ai didi

python - ValueError : view limit minimum -35738. 3640567 小于 1 并且是无效的 Matplotlib 日期值

转载 作者:行者123 更新时间:2023-11-30 21:55:01 25 4
gpt4 key购买 nike

我尝试使用matplotlib.pyplot绘制数据框,但在绘制时出现以下错误:

ValueError: view limit minimum -35738.3640567 is less than 1 and is an invalid Matplotlib date value. This often happens if you pass a non-datetime value to an axis that has datetime units.

根据错误,'datetime' 列中似乎有一个非日期时间值,但实际上没有。

我尝试使用 pd.to_datetime() 更改时间戳的格式 pd.to_datetime(df_google['datetime'], format = '%d/%m/%Y'),但没有任何变化。

这是代码:

import matplotlib.pyplot as plt

df_google.plot()
plt.show()

df_google 是一个包含列 ['datetime', 'price'] 的数据框,其中一些值如下:

     datetime        price
0 2018-05-15 1079.229980
1 2018-05-16 1081.770020
2 2018-05-17 1078.589966
3 2018-05-18 1066.359985
4 2018-05-21 1079.579956
5 2018-05-22 1069.729980
6 2018-05-23 1079.689941
7 2018-05-24 1079.239990
8 2018-05-25 1075.660034
9 2018-05-29 1060.319946

有人可以尝试帮助我理解这种类型的错误吗?当每个值都是日期时间 Dtype 值时,为什么会说存在非日期时间值?我怎样才能绘制这个数据框?

最佳答案

  • 'datetime' 列设置为 datetime64[ns] Dtype 以解决错误:
    • 使用pandas.to_datetime转换'datetime'列,并记住将该列分配回其自身,因为这不是就地更新。
    • pandas 擅长计算日期时间的格式,但可能需要使用 format= 选项指定当前格式'datetime' 列的 >。请参阅Convert Pandas Column to DateTime .
  • 如果列名不包含特殊字符,并且不与内置属性/方法冲突(例如,index计数)。
    • df_google.datetime 而不是 df_google['datetime']
import pandas as pd
import matplotlib.pyplot as plt

# given the following data
data = {'datetime': ['2018-05-15', '2018-05-16', '2018-05-17', '2018-05-18', '2018-05-21', '2018-05-22', '2018-05-23', '2018-05-24', '2018-05-25', '2018-05-29'],
'price': [1079.22998, 1081.77002, 1078.589966, 1066.359985, 1079.579956, 1069.72998, 1079.689941, 1079.23999, 1075.660034, 1060.319946]}

df_google = pd.DataFrame(data)

# convert the datetime column to a datetime type and assign it back to the column
df_google.datetime = pd.to_datetime(df_google.datetime)

# display(df_google.head())
datetime price
0 2018-05-15 1079.229980
1 2018-05-16 1081.770020
2 2018-05-17 1078.589966
3 2018-05-18 1066.359985
4 2018-05-21 1079.579956
5 2018-05-22 1069.729980
6 2018-05-23 1079.689941
7 2018-05-24 1079.239990
8 2018-05-25 1075.660034
9 2018-05-29 1060.319946

验证'datetime'列是datetime64[ns] Dtype:

print(df_google.info())

<class 'pandas.core.frame.DataFrame'>
RangeIndex: 10 entries, 0 to 9
Data columns (total 2 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 datetime 10 non-null datetime64[ns]
1 price 10 non-null float64
dtypes: datetime64[ns](1), float64(1)
memory usage: 288.0 bytes

情节:

  • 使用 x=... 指定要绘制的轴列。
df_google.plot(x='datetime')
plt.show()
  • 也可以通过df.set_index('datetime', inplace=True)将要作为x轴的列设置为索引,然后df.plot() 来绘图,但设置索引不是必需的,并且与错误无关。
  • 有一个庞大的替代绘图工具生态系统,但 df.plot() 非常适合查看数据。

enter image description here

注意:

关于python - ValueError : view limit minimum -35738. 3640567 小于 1 并且是无效的 Matplotlib 日期值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57874226/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com