Seaborn 的异常 - Pandas:AttributeError:'DatetimeIndex' 对象没有属性'mean'
Posted
技术标签:
【中文标题】Seaborn 的异常 - Pandas:AttributeError:\'DatetimeIndex\' 对象没有属性\'mean\'【英文标题】:Exception with Seaborn - Pandas : AttributeError: 'DatetimeIndex' object has no attribute 'mean'Seaborn 的异常 - Pandas:AttributeError:'DatetimeIndex' 对象没有属性'mean' 【发布时间】:2019-06-12 13:03:25 【问题描述】:我已经在日期时间索引上分组了一个数据集,现在我试图在 seaborn 中绘制一个 regplot,但我得到了例外:
AttributeError: 'DatetimeIndex' object has no attribute 'mean'
这是我的代码和数据:
data_grouped_by_month_sum = \
data.groupby('Date')[['revenue', 'discount', 'profit', 'unit_sales']].sum().copy()
data_grouped_by_month_sum
revenue discount profit unit_sales
Date
2017-01-01 3.893144e+07 -11659943 1.183623e+07 284106.000
2017-01-02 2.830647e+07 -7562670 8.830296e+06 207903.000
2017-01-03 3.545951e+07 -6557402 1.107279e+07 230452.000
2017-01-04 4.045753e+07 -7170590 1.202627e+07 248411.000
2017-01-05 3.424532e+07 -4629413 1.144603e+07 224402.000
2017-01-06 3.870204e+07 -8969420 1.233788e+07 265580.000
2017-01-07 4.337187e+07 -12686255 1.320915e+07 305822.000
2017-01-08 3.358365e+07 -7093865 1.102919e+07 228076.000
2017-01-09 4.180871e+07 -10448234 1.299629e+07 282168.000
2017-01-10 3.673581e+07 -7465803 1.227295e+07 250831.590
2017-01-11 3.906452e+07 -9699514 1.201579e+07 267460.810
2017-01-12 3.973008e+07 -8881891 1.307094e+07 256409.000
2018-01-01 4.162616e+07 -15622350 1.268677e+07 301832.430
2018-01-02 2.875557e+07 -7975778 9.489892e+06 205916.000
2018-01-03 3.483879e+07 -7647761 1.156390e+07 229373.094
2018-01-04 4.104401e+07 -8871760 1.337660e+07 261226.000
2018-01-05 3.442540e+07 -6031865 1.156874e+07 216652.000
2018-01-06 4.261711e+07 -11704861 1.344429e+07 271074.000
2018-01-07 4.424721e+07 -16336176 1.341742e+07 297929.000
2018-01-08 4.073636e+07 -11554053 1.352638e+07 265206.000
2018-01-09 4.233467e+07 -11154759 1.390979e+07 273736.000
2018-01-10 3.748328e+07 -8015935 1.302983e+07 240237.000
2018-01-11 4.305011e+07 -12113901 1.315972e+07 257665.000
2018-01-12 4.399236e+07 -12608672 1.400170e+07 268436.000
data_grouped_by_month_sum.to_json()
'"revenue":"1483228800000":38931444.750042133,"1483315200000":28306467.9100316688,"1483401600000":35459508.4300272688,"1483488000000":40457531.5200371891,"1483574400000":34245317.4000276178,"1483660800000":38702042.4600370005,"1483747200000":43371873.3400478438,"1483833600000":33583649.6600372717,"1483920000000":41808710.6400365829,"1484006400000":36735805.9300293103,"1484092800000":39064522.9900306687,"1484179200000":39730082.4300418273,"1514764800000":41626157.1400516033,"1514851200000":28755571.640029829,"1514937600000":34838788.8400268629,"1515024000000":41044009.2700255737,"1515110400000":34425402.8400360122,"1515196800000":42617106.1200316921,"1515283200000":44247207.1600385979,"1515369600000":40736360.9100449458,"1515456000000":42334671.940039821,"1515542400000":37483276.4300348833,"1515628800000":43050105.7500336245,"1515715200000":43992355.770037964,"discount":"1483228800000":-11659943,"1483315200000":-7562670,"1483401600000":-6557402,"1483488000000":-7170590,"1483574400000":-4629413,"1483660800000":-8969420,"1483747200000":-12686255,"1483833600000":-7093865,"1483920000000":-10448234,"1484006400000":-7465803,"1484092800000":-9699514,"1484179200000":-8881891,"1514764800000":-15622350,"1514851200000":-7975778,"1514937600000":-7647761,"1515024000000":-8871760,"1515110400000":-6031865,"1515196800000":-11704861,"1515283200000":-16336176,"1515369600000":-11554053,"1515456000000":-11154759,"1515542400000":-8015935,"1515628800000":-12113901,"1515715200000":-12608672,"profit":"1483228800000":11836229.2489999644,"1483315200000":8830296.2539999764,"1483401600000":11072792.1739999857,"1483488000000":12026270.4389999546,"1483574400000":11446032.3169999532,"1483660800000":12337875.1799999755,"1483747200000":13209154.8969999738,"1483833600000":11029193.314999992,"1483920000000":12996287.9609999713,"1484006400000":12272951.6589999795,"1484092800000":12015785.5899999943,"1484179200000":13070944.6019999497,"1514764800000":12686767.6370000038,"1514851200000":9489891.6859999932,"1514937600000":11563900.9729999918,"1515024000000":13376596.2279999945,"1515110400000":11568739.8190000076,"1515196800000":13444291.7750000041,"1515283200000":13417418.8410000205,"1515369600000":13526381.3719999716,"1515456000000":13909793.1799999997,"1515542400000":13029831.0179999936,"1515628800000":13159723.2340000197,"1515715200000":14001704.0719999988,"unit_sales":"1483228800000":284106.0,"1483315200000":207903.0,"1483401600000":230452.0,"1483488000000":248411.0,"1483574400000":224402.0,"1483660800000":265580.0,"1483747200000":305822.0,"1483833600000":228076.0,"1483920000000":282168.0,"1484006400000":250831.59,"1484092800000":267460.81,"1484179200000":256409.0,"1514764800000":301832.43,"1514851200000":205916.0,"1514937600000":229373.094,"1515024000000":261226.0,"1515110400000":216652.0,"1515196800000":271074.0,"1515283200000":297929.0,"1515369600000":265206.0,"1515456000000":273736.0,"1515542400000":240237.0,"1515628800000":257665.0,"1515715200000":268436.0'
sns.regplot(x = data_grouped_by_month_sum.index, y = 'revenue', data = data_grouped_by_month_sum)
AttributeError: 'DatetimeIndex' object has no attribute 'mean'
更新
在实施收到的建议时,我得到以下信息:
此图有两个问题:a) 这些点似乎聚集在两个离散的时间点上,但事实并非如此,b) 水平线不可解释。
如果我采用更简单的方法,我会得到以下图表:
【问题讨论】:
【参考方案1】:DatetimeIndex
对象没有错误回溯中给出的mean
属性。
首先要做的是将x
的正确值传递给seaborn.regplot
函数。
据记载,这可以是string, series or vector array。
DatetimeIndex
对象有一个to_series
方法,可用于将其转换为pandas.core.series.Series
对象。
在对DatetimeIndex
对象调用to_series
方法之前,必须将DatetimeIndex
中的pandas._libs.tslibs.timestamps.Timestamp
值转换为seaborn.regplot
可以使用的时间戳值。
import pandas as pd
x = data_grouped_by_month_sum.index.map(pd.Timestamp.timestamp).to_series()
sns.regplot(x=x, y='revenue', data=data_grouped_by_month_sum)
您可以将时间戳值转换回以显示适合 x 轴 的人类可读标签。
【讨论】:
你能看看我更新的帖子和评论吗?谢谢。 看图,收入数据不是正态分布的。除非您想假设收入随时间的正态分布,否则将 x 设置为一个大小与数据行相同的数组在这里是有意义的。以上是关于Seaborn 的异常 - Pandas:AttributeError:'DatetimeIndex' 对象没有属性'mean'的主要内容,如果未能解决你的问题,请参考以下文章
同一图上 Pandas 数据框多列的箱线图(seaborn)