Himal Acharya
Himal Acharya

Reputation: 1006

Grouping large GPS data by 1 min

date          object
lat          float64
lon          float64
speed        float64
direction    float64

In my csv file ,date is of following format 2016-04-29 11:45:21 It shows date as object type . There are more than 10 records in every minute. so, I want to group together and apply mean of speed for every 1 min GPS data. I try following code where datafile is pandas dataframe.

datafile.groupby(pd.TimeGrouper('1Min'))['speed'].mean()

The following error comes:

TypeError: axis must be a DatetimeIndex, but got an instance of 'Int64Index'


after editing as in comment and then datafile.head() showstable outputtaxi table output after datafile.head()

Now i there are 1069 records from 06:35:20 to 06:59:59. I need to find the avg of speed of every 1 min data

Upvotes: 1

Views: 97

Answers (1)

Stefan
Stefan

Reputation: 42875

You need to create a DateTimeIndex from your data column using:

df.index = pd.to_datetime(df.loc[: 'date'], format='%Y-%m-%d %H:%M:%S')

However, you can probably leverage the builtin .read_csv() functionality, using parse_dates=True and index_col=0 to read date as index and then parse the index (assuming date is first column).

Upvotes: 1

Related Questions