DataFrame中的日期格式更改

时间:2018-09-22 16:12:20

标签: python pandas date dataframe format

在我读取文件并解析日期之后,日期格式在dataframe.代码下方的v2x = r'E:\Model\Data\v2x.csv' outfile = r'E:\Model\ModelSpecific\Input_shat2.txt' df_data = pd.read_csv(file_name,parse_dates=[0], index_col=0) df_v2x = pd.read_csv(v2x, parse_dates=[0], sep=",") print(df_v2x[4800:5000]) 查找中发生了变化。

4988 2018-07-08   V2TX     12.6265 --> Wrong Format
4989 2018-08-08   V2TX     12.8654
4990 2018-09-08   V2TX     12.4882
4991 2018-10-08   V2TX     15.1113 
4992 2018-08-13   V2TX     15.9406 --> Right Format
4993 2018-08-14   V2TX     15.8610
4994 2018-08-15   V2TX     18.4755
4995 2018-08-16   V2TX     16.2633

正确的格式应为'%y-%m-%d'

打印输出:

UICollectionView

感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

找到了一种具有“烦人”解决方法的解决方案(提取日期,月份和年份的字符串)。

v2x = r'E:\Model\Data\v2x.csv'
outfile = r'E:\Model\ModelSpecific\Input_shat2.txt'

data = pd.read_csv(v2x, sep=",")

data['Year'] = data['Date'].str.slice(6, 10)  #redo the index because of american timestamp
data['Month'] = data['Date'].str.slice(3,5) 
data['Day'] = data['Date'].str.slice(0,2)
datetime = pd.to_datetime(data[['Year','Month','Day']])
data = data.drop(['Date','Year','Month','Day'],axis=1)
data = pd.concat((datetime,data),axis=1)
data = data.rename({0:'Date'},axis=1)
data = data.set_index('Date')