Python 将数据类型对象转换为字符串

Python 将数据类型对象转换为字符串,python,pandas,Python,Pandas,转换列的数据类型时遇到问题。我正在从yahoo finance加载csv文件 dt = pd.read_csv('data/Tesla.csv') 这给了我以下信息: <class 'pandas.core.frame.DataFrame'> Int64Index: 923 entries, 0 to 922 Data columns (total 7 columns): Date 923 non-null object Open 923 non-n

转换列的数据类型时遇到问题。我正在从yahoo finance加载csv文件

dt = pd.read_csv('data/Tesla.csv')
这给了我以下信息:

<class 'pandas.core.frame.DataFrame'>
Int64Index: 923 entries, 0 to 922
Data columns (total 7 columns):
Date         923 non-null object
Open         923 non-null float64
High         923 non-null float64
Low          923 non-null float64
Close        923 non-null float64
Volume       923 non-null int64
Adj Close    923 non-null float64
dtypes: float64(5), int64(1), object(1)
但似乎一切都不起作用


我使用pandas版本0.13.1

将日期转换为日期时间,可以方便地将用户输入的日期与数据中的日期进行比较

#Load in the data
dt = pd.read_csv('data/Tesla.csv')

#Change the 'Date' column into DateTime
dt['Date']=pd.to_datetime(dt['Date'])

#Find a Date using strings
np.where(dt['Date']=='2014-02-28')
#returns     (array([0]),)

np.where(dt['Date']=='2014-02-21')
#returns (array([5]),)

#To get the entire row's information
index = np.where(dt['Date']=='2014-02-21')[0][0]
dt.iloc[index]

#returns:
Date         2014-02-21 00:00:00
Open                      211.64
High                      213.98
Low                       209.19
Close                      209.6
Volume                   7818800
Adj Close                  209.6
Name: 5, dtype: object
因此,如果要执行for循环,可以创建一个日期列表或numpy数组,然后遍历它们,用值替换索引中的日期:

input = np.array(['2014-02-21','2014-02-28'])
for i in input:
    index = np.where(dt['Date']==i)[0][0]
    dt.iloc[index]

object
d键入可变长度字符串的表示方式。你到底想做什么?我想将数据框中的日期与输入字段给出的日期进行比较,输入字段是一个字符串。我需要比较两者以向用户提供正确的信息。可以在这里找到数据可以在这里找到数据我用pd.to_datetime()转换了日期列,以便循环我使用的行:对于范围内的I(len(tesla),5):打印类型((tesla.iloc[[I]]['Date']))这给了我一个类型的变量:我还使用:datetime.strtime('2013-08-20','%Y-%M-%d')将字符串转换为datetime。这给了我现在需要将转换后的字符串与for循环中的值进行比较的机会。我离得越来越近了,我只想获取该特定行中的所有信息。这就是为什么在for循环中使用tesla.iloc[[i]]['Date']的原因。
input = np.array(['2014-02-21','2014-02-28'])
for i in input:
    index = np.where(dt['Date']==i)[0][0]
    dt.iloc[index]