为什么我无法创建熊猫专栏。 TypeError:“ NoneType”对象不可迭代

时间:2020-09-25 16:39:49

标签: python pandas dataframe jupyter-notebook jupyter-lab

我正在YouTube上使用sendex的机器学习教程,并尝试复制他的代码。

import pandas as pd
import numpy as np
import quandl

df = quandl.get("WIKI/GOOGL")
df = df.iloc[:,7:]
df['HL_PLT'] = (df['Adj. High'] - df['Adj. Low']) / df['Adj. High'] * 100
df['PCT_change'] = (df['Adj. Close'] - df['Adj. Open']) / df['Adj. Open'] * 100

我遇到错误

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-32-7b605dbbfdfe> in <module>
      1 df = quandl.get("WIKI/GOOGL")
      2 df = df.iloc[:,7:]
----> 3 df['HL_PLT'] = (df['Adj. High'] - df['Adj. Low']) / df['Adj. High'] * 100
      4 df['PCT_change'] = (df['Adj. Close'] - df['Adj. Open']) / df['Adj. Open'] * 100

~\Anaconda3\lib\site-packages\pandas\core\frame.py in __setitem__(self, key, value)
   3026 
   3027         # see if we can slice the rows
-> 3028         indexer = convert_to_index_sliceable(self, key)
   3029         if indexer is not None:
   3030             # either we have a slice or we have a string that can be converted

~\Anaconda3\lib\site-packages\pandas\core\indexing.py in convert_to_index_sliceable(obj, key)
   2144         if idx._supports_partial_string_indexing:
   2145             try:
-> 2146                 return idx._get_string_slice(key)
   2147             except (KeyError, ValueError, NotImplementedError):
   2148                 return None

~\Anaconda3\lib\site-packages\pandas\core\indexes\datetimes.py in _get_string_slice(self, key, use_lhs, use_rhs)
    678     def _get_string_slice(self, key: str, use_lhs: bool = True, use_rhs: bool = True):
    679         freq = getattr(self, "freqstr", getattr(self, "inferred_freq", None))
--> 680         parsed, reso = parsing.parse_time_string(key, freq)
    681         reso = Resolution.from_attrname(reso)
    682         loc = self._partial_date_slice(reso, parsed, use_lhs=use_lhs, use_rhs=use_rhs)

pandas\_libs\tslibs\parsing.pyx in pandas._libs.tslibs.parsing.parse_time_string()

pandas\_libs\tslibs\parsing.pyx in pandas._libs.tslibs.parsing.parse_datetime_string_with_reso()

pandas\_libs\tslibs\parsing.pyx in pandas._libs.tslibs.parsing.dateutil_parse()

TypeError: 'NoneType' object is not iterable

我真正好奇的是,无需创建列,代码就可以很好地运行

(df['Adj. High'] - df['Adj. Low']) / df['Adj. High'] * 100
(df['Adj. Close'] - df['Adj. Open']) / df['Adj. Open'] * 100
Date
2004-08-19    0.324968
2004-08-20    7.227007
2004-08-23   -1.227880
2004-08-24   -5.726357
2004-08-25    1.183658
                ...   
2018-03-21    0.130884
2018-03-22   -2.487014
2018-03-23   -2.360729
2018-03-26    0.332191
2018-03-27   -5.353887
Length: 3424, dtype: float64

即使使用一些变量名,该列仍然可以成功创建

df['1'] = (df['Adj. High'] - df['Adj. Low']) / df['Adj. High'] * 100
df['2'] = (df['Adj. Close'] - df['Adj. Open']) / df['Adj. Open'] * 100
df

Adj. Open   Adj. High   Adj. Low    Adj. Close  Adj. Volume 1   2
Date                            
2004-08-19  50.159839   52.191109   48.128568   50.322842   44659000.0  7.783971    0.324968
2004-08-20  50.661387   54.708881   50.405597   54.322689   22834300.0  7.865787    7.227007
2004-08-23  55.551482   56.915693   54.693835   54.869377   18256100.0  3.903772    -1.227880
2004-08-24  55.792225   55.972783   51.945350   52.597363   15247300.0  7.195341    -5.726357
2004-08-25  52.542193   54.167209   52.100830   53.164113   9188600.0   3.814815    1.183658
... ... ... ... ... ... ... ...
2018-03-21  1092.570000 1108.700000 1087.210000 1094.000000 1990515.0   1.938306    0.130884
2018-03-22  1080.010000 1083.920000 1049.640000 1053.150000 3418154.0   3.162595    -2.487014
2018-03-23  1051.370000 1066.780000 1024.870000 1026.550000 2413517.0   3.928645    -2.360729
2018-03-26  1050.600000 1059.270000 1010.580000 1054.090000 3272409.0   4.596562    0.332191
2018-03-27  1063.900000 1064.540000 997.620000  1006.940000 2940957.0   6.286283    -5.353887
3424 rows × 7 columns

列名'a'也可以很好地创建,但是列名'b'或比其长的任何内容都会产生错误。

'TypeError:'NoneType'对象不可迭代'

0 个答案:

没有答案