Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/348.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 设置索引和重置索引可更改变量的类型_Python_Pandas - Fatal编程技术网

Python 设置索引和重置索引可更改变量的类型

Python 设置索引和重置索引可更改变量的类型,python,pandas,Python,Pandas,这是我的熊猫数据框 import datetime as dt d1=dt.date(2021,1,1) d2=dt.date(2021,1,13) df = pd.DataFrame({'date':[d1,d2],'location':['Paris','New York'],'Number':[2,3]}) 这是我的问题 df = df.set_index(['date']) df = df.reset_index() print(df.loc[df.date == d1]) # Ok

这是我的熊猫数据框

import datetime as dt
d1=dt.date(2021,1,1)
d2=dt.date(2021,1,13)
df = pd.DataFrame({'date':[d1,d2],'location':['Paris','New York'],'Number':[2,3]})
这是我的问题

df = df.set_index(['date'])
df = df.reset_index()
print(df.loc[df.date == d1]) # Ok !
df = df.set_index(['location','date'])  # order has not importance
df = df.reset_index()
print(df.loc[df.date == d1]) # Not OK ! it returns an Empty DataFrame 
当我用两列设置_索引并重置_索引时,日期类型似乎丢失了


我不明白为什么它在第一种情况下工作,而不是在第二种情况下,我需要执行pd.to_datetime(df['date']].dt.date才能再次工作?

因为它将列类型更改为
datetime64[ns]
并且单个值的输出是
Timestamp
我们需要添加日期转换

df.date[0]
Out[175]: Timestamp('2021-01-01 00:00:00')

df.loc[df.date.dt.date == d1]
Out[177]: 
  location       date  Number
0    Paris 2021-01-01       2

为了解释差异,我们可以检查元素的数据类型并查看差异

在不同阶段对数据帧应用类型函数:

df.applymap(type)
从下面可以看出,最后阶段的数据类型不同。因此,在最后阶段没有对手

输出:

import datetime as dt
d1=dt.date(2021,1,1)
d2=dt.date(2021,1,13)
df = pd.DataFrame({'date':[d1,d2],'location':['Paris','New York'],'Number':[2,3]})

df.applymap(type)

                      date       location         Number
0  <class 'datetime.date'>  <class 'str'>  <class 'int'>      # <=== datetime.date
1  <class 'datetime.date'>  <class 'str'>  <class 'int'>      # <=== datetime.date

df = df.set_index(['date'])
df = df.reset_index()
print(df.loc[df.date == d1]) # Ok !

df.applymap(type)

                      date       location         Number
0  <class 'datetime.date'>  <class 'str'>  <class 'int'>      # <=== datetime.date
1  <class 'datetime.date'>  <class 'str'>  <class 'int'>      # <=== datetime.date


df = df.set_index(['location','date'])  # order has not importance
df = df.reset_index()
print(df.loc[df.date == d1]) # Not OK ! it returns an Empty DataFrame 

df.applymap(type)

        location                                                date         Number
0  <class 'str'>  <class 'pandas._libs.tslibs.timestamps.Timestamp'>  <class 'int'>         # <=== data type changed to pandas._libs.tslibs.timestamps.Timestamp 
1  <class 'str'>  <class 'pandas._libs.tslibs.timestamps.Timestamp'>  <class 'int'>         # <=== data type changed to pandas._libs.tslibs.timestamps.Timestamp 

将日期时间导入为dt
d1=dt.日期(2021,1,1)
d2=日期(2021,1,13)
df=pd.DataFrame({'date':[d1,d2],'location':['Paris','newyork'],'Number':[2,3]})
df.applymap(类型)
日期位置号
0            #