Python 名称错误:名称';期间';没有定义

Python 名称错误:名称';期间';没有定义,python,pandas,Python,Pandas,我尝试从现有数据帧创建示例数据帧,代码如下: import datetime as dt df1 = pd.DataFrame( {'YearMonth': {0: Period('2020-07', 'M'), 1: Period('2020-06', 'M')}, 'State': {0: 'BBB', 1: 'AAA'}} ) 但它发现了一个错误: ---------------------------------------------------------------------

我尝试从现有数据帧创建示例数据帧,代码如下:

import datetime as dt
df1 = pd.DataFrame(
{'YearMonth': {0: Period('2020-07', 'M'), 1: Period('2020-06', 'M')},
 'State': {0: 'BBB', 1: 'AAA'}}
)
但它发现了一个错误:

---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-98-7d7247456452> in <module>
      2 df1 = pd.DataFrame(
      3 
----> 4 {'YearMonth': {0: Period('2020-07', 'M'), 1: Period('2020-06', 'M')},
      5  'State': {0: 'BBB', 1: 'AAA'}}
      6 

NameError: name 'Period' is not defined
---------------------------------------------------------------------------
NameError回溯(最近一次呼叫上次)
在里面
2 df1=pd.DataFrame(
3.
---->4{'YearMonth':{0:Period('2020-07','M'),1:Period('2020-06','M'),
5'状态':{0:'BBB',1:'AAA'}
6.
NameError:未定义名称“Period”

似乎是导入错误,请使用导入期间的

任一

from pandas import Period
df1 = pd.DataFrame(
{'YearMonth': {0: Period('2020-07', 'M'), 1: Period('2020-06', 'M')},
 'State': {0: 'BBB', 1: 'AAA'}}
)
df1
import pandas as pd

df1 = pd.DataFrame(
{'YearMonth': {0: pd.Period('2020-07', 'M'), 1: pd.Period('2020-06', 'M')},
 'State': {0: 'BBB', 1: 'AAA'}}
)
df1

from pandas import Period
df1 = pd.DataFrame(
{'YearMonth': {0: Period('2020-07', 'M'), 1: Period('2020-06', 'M')},
 'State': {0: 'BBB', 1: 'AAA'}}
)
df1
import pandas as pd

df1 = pd.DataFrame(
{'YearMonth': {0: pd.Period('2020-07', 'M'), 1: pd.Period('2020-06', 'M')},
 'State': {0: 'BBB', 1: 'AAA'}}
)
df1
输出

    YearMonth   State
0   2020-07     BBB
1   2020-06     AAA

np,祝你一切顺利!