3

I can confirm I set the index to my dataframe

df.set_index('time_date', inplace=True)
df.head()
                  Unnamed: 0    lid t_user_id   collected_time  latitude    longitude   altitude    transportation_mode
time_date                               
2008-04-01 11:30:37-03:00   0   1   10  2008-04-01 11:30:37-03  39.475128   75.999173   -777.0  walk
2008-04-01 11:31:38-03:00   1   1   10  2008-04-01 11:31:38-03  39.474785   75.999100   -777.0  walk
2008-04-01 11:32:37-03:00   2   1   10  2008-04-01 11:32:37-03  39.474385   75.999417   -777.0  walk
2008-04-01 11:33:36-03:00   3   1   10  2008-04-01 11:33:36-03  39.473852   75.999690   -777.0  walk
2008-04-01 11:34:35-03:00   4   1   10  2008-04-01 11:34:35-03  39.473417   76.000253   -777.0  walk

However, everytime I pass the dataframe to a myclass object, I get this error message.

ts_obj = ts.TrajectorySegmentation(df)
ts_obj.load_data()
65         # sort data first
     66         #self.raw_data=self.raw_data.sort_index()
---> 67         self.row_data['day'] = self.row_data.index.date
     68 
     69         # preprocessing

AttributeError: 'Index' object has no attribute 'date'

Class definition:enter code here

class TrajectorySegmentation:
    def __init__(self, rowData=pd.DataFrame()):
        self.row_data = rowData

    def load_data(self, **kwargs):
        src = self.row_data

I cannot figure out what wrong with the line 67 in the library that python complain about, but here is fragment containing line 67

65  # sort data first
66  #self.raw_data=self.raw_data.sort_index()
67  self.row_data['day'] = self.row_data.index.date
68

EDIT

Tried one of the answers, same error, screenshot: enter image description here

EDIT-2

sample data

!cat sample.csv
time_date,Unnamed: 0,lid,t_user_id,collected_time,latitude,longitude,altitude,transportation_mode
2008-04-01 11:30:37-03:00,0,1,10,2008-04-01 11:30:37-03,39.47512800000001,75.999173,-777.0,walk
2008-04-01 11:31:38-03:00,1,1,10,2008-04-01 11:31:38-03,39.474785,75.9991,-777.0,walk
2008-04-01 11:32:37-03:00,2,1,10,2008-04-01 11:32:37-03,39.474385,75.99941700000002,-777.0,walk
2008-04-01 11:33:36-03:00,3,1,10,2008-04-01 11:33:36-03,39.473852,75.99969,-777.0,walk
2008-04-01 11:34:35-03:00,4,1,10,2008-04-01 11:34:35-03,39.473417,76.000253,-777.0,walk
1
  • Your datetime format seems incorrect. Try to get it to a datetime format? Also check by doing an iloc or dtypes before setting index. Commented Jun 6, 2020 at 0:51

3 Answers 3

3

It seems like your time_date column isn't being converted to a datetime64 object. Try adding utc=True to pd.to_datetime.

This snippet works:

import pandas as pd

df = pd.read_csv('sample.csv', delimiter=',', header=0, index_col=False)

# convert time_date col to datetime64 dtype
df['time_date'] = pd.to_datetime(df['time_date'], utc=True)

df.set_index('time_date', inplace=True)

print(df.index.date)

Output

[datetime.date(2008, 4, 1) datetime.date(2008, 4, 1)
 datetime.date(2008, 4, 1) datetime.date(2008, 4, 1)
 datetime.date(2008, 4, 1)]

Edit: You can use df.info() to double check that your columns are the correct dtypes.

Sign up to request clarification or add additional context in comments.

Comments

0

My guess is you set index before making time_date a datetime object. Please try

Data based on your post

df=pd.DataFrame({'collected_time':['2008-04-01 11:30:37-03','2008-04-01 11:31:38-03','2008-04-02 11:32:37-03','2008-04-01 11:33:36-03'],'val':[23,76,2,45]})

Coerce collected_time to datetime

df['collected_time']=pd.to_datetime(df['collected_time'])
df.dtypes#Check dataframe data types

#Set Index

df.set_index('collected_time', inplace=True)

Extract date from index

df['date']=df.index.date

enter image description here

2 Comments

That should work if all the values of time_date are as shown. Can you please post sample data with a variety of dates that is if the dates are varied
Cant make sense of recent data posted. Do you want to copy the date object you want indexed and maybe one other colum and put as sample. Else, can follow my rework above.
0

Maybe you can work around it by using the apply function on the Pandas series passing in the function lambda x: x.date?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.