【发布时间】:2015-03-11 00:55:30
【问题描述】:
使用.to_csv method to convert Dataframes 时。我得到日期字符串的随机日期格式。
对于 csv 中的给定列,日期格式奇怪地在 mm/dd/yy 和 mm-dd-yyyy 之间波动。原始数据框没有这个问题。
.csv 文件中的示例列
03-01-2014
10/19/13
04-11-2014
09/19/14
09/19/14
12/18/13
03-10-2013
04/13/14
我使用下面的代码来生成日期列
def Get_random_date():
t=datetime.datetime.now()
mon=t.month
dy=t.day
yr=t.year
year = random.choice(range(2013, yr+1))
month = random.choice(range(1, mon+1))
day = random.choice(range(1, 28))
return pd.datetime(year,month,day)
def Generate_Dates():
DateCreated = pd.Series([])
date_created=Get_random_date()
DateCreated=date_created.strftime('%x')
return (DateCreated)
...
row=pd.DataFrame(index=index,columns=dt.columns)
row['Date Created']=Generate_Dates()
...
new_dt.to_csv('new_data.csv')
.csv 文件中的示例列
03-01-2014
10/19/13
04-11-2014
09/19/14
09/19/14
12/18/13
03-10-2013
04/13/14
【问题讨论】:
标签: python date pandas format dataframe