Append/Replace a row in a pandas DataFrame [SOLVED]
Peter Otten
__peter__ at web.de
Thu Apr 14 03:11:38 EDT 2016
Paulo da Silva wrote:
> Às 21:10 de 13-04-2016, Paulo da Silva escreveu:
>> Hi all.
> ...
>>> [6 rows x 4 columns]
>>>>> dft=pd.DataFrame([[1,2,3,4]],
>> index=[datetime.date(2016,1,12)],columns=df.columns)
>>>>> dft
>> A B C D
>> 2016年01月12日 1 2 3 4
>>>> [1 rows x 4 columns]
>>>>> pd.concat([df,dft])
>> Out[71]:
>> A B C D
>> 2013年01月01日 00:00:00 -0.111621 1.126761 -2.420517 0.660948
>> 2013年01月02日 00:00:00 -0.243397 -0.975684 -0.679209 -0.656913
>> 2013年01月03日 00:00:00 0.405816 0.478353 0.621906 -0.262615
>> 2013年01月04日 00:00:00 -0.380249 0.416711 -0.906286 1.828339
>> 2013年01月05日 00:00:00 0.772747 0.993784 0.452746 1.665306
>> 2013年01月06日 00:00:00 0.535011 -0.662874 1.504281 0.543537
>> 2016年01月12日 1.000000 2.000000 3.000000 4.000000
>>>> [7 rows x 4 columns]
>>>> Why am I getting the second column?!
> I need to use for example pd.datetime instead of datetime.date. In fact
> there is no extra col but the inclusion of hour in the index.
> Still don't understand why!
It looks like handling of datetime objects is specialised
>>> pd.DataFrame([[1,2]], index=[datetime.datetime.now()], columns=["foo",
"bar"]).index
<class 'pandas.tseries.index.DatetimeIndex'>
[2016年04月14日 08:51:09.192283]
Length: 1, Freq: None, Timezone: None
and assumes the "all-midnight" case to mean that no time of day was provided
whereas date is handled like a generic object
>>> pd.DataFrame([[1,2]], index=[datetime.date.today()], columns=["foo",
"bar"]).index
Index([2016年04月14日], dtype='object')
and triggers usage of the string conversion provided by the entries.
For datetime this includes the time that you mistook for an extra column.
>>> str(datetime.datetime(2010, 12, 21))
'2010-12-21 00:00:00'
>>> str(datetime.date(2010, 12, 21))
'2010-12-21'
>> How do I do to have a row replaced instead of added if its date (index)
>> is an existent one?
> df.loc[<the index>]=<the new/replacement list/tuple line>
>> Paulo
>
More information about the Python-list
mailing list