Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create empy pandas DataFrame with DateTimeIndex for random time delta values

Im trying to create an empty DataFrame for which I will then constantly be appending rows to using the time stamp when the data arrives as index.

This is to code I have so far:

import pandas as pd
import datetime
df = pd.DataFrame(columns=['a','b'],index=pd.DatetimeIndex(freq='s'))
df.loc[event.get_datetime()] = event.get_data()

The problem Im having is with freq in the DateTimeIndex, the data is not arriving at any predefined intervalls, it is ju when some event tiggers. And also in the code above I need to specify a start and enddate for the index I dont want that I just want to be able to append rows whenever they arrive.

like image 815
user3139545 Avatar asked Jun 10 '18 21:06

user3139545


2 Answers

Set up empty with pd.to_datetime

df = pd.DataFrame(columns=['a','b'], index=pd.to_datetime([]))

Then do this

df.loc[pd.Timestamp('now')] = pd.Series([1, 2], ['a', 'b'])
df

                            a  b
2018-06-10 20:52:52.025426  1  2
like image 96
piRSquared Avatar answered Sep 28 '22 08:09

piRSquared


The first argument of DateTimeIndex is data. Try setting data to an empty list. If you want to define the start time, end time, or frequency, take a look at the other arguments for DateTimeIndex.

df = pd.DataFrame(columns=['a','b'], index=pd.DatetimeIndex([], name='startime'))

If you're trying to index on time delta values, also consider

df = pd.DataFrame(columns=['a','b'], index=pd.TimedeltaIndex([]))
like image 22
kdauria Avatar answered Sep 28 '22 10:09

kdauria