Skip to content Skip to sidebar Skip to footer

Need Work-around For Handling Timestamps In Dataframe And Get Datetime

I originally posted a question about plotting different datetime-sampling in the same plot, stored in many different dataframes. I got help understanding I needed to convert my tim

Solution 1:

I guess I am having trouble figuring out what you are asking. Given a df of the form:

tsvalue02019-10-18 08:13:26.702 1412019-10-18 08:13:26.765 1022019-10-18 08:13:26.790 532019-10-18 08:13:26.889 642019-10-18 08:13:26.901 852019-10-18 08:13:27.083 33

I can execute the following to convert the ts column to a pd.datetime varaible and make the ts column the index:

df['ts'] = pd.to_datetime(df['ts'])
df = df.set_index(['ts'], drop=True)

which yields the df of form

valuets2019-10-18 08:13:26.702 142019-10-18 08:13:26.765 102019-10-18 08:13:26.790 52019-10-18 08:13:26.889 62019-10-18 08:13:26.901 8

I can then print the values of the index, or for that matter use any iteration on the index I want. The following just gives the first 5 values.

foriinrange(5):print(df.iloc[i].name)2019-10-18 08:13:26.7020002019-10-18 08:13:26.7650002019-10-18 08:13:26.7900002019-10-18 08:13:26.8890002019-10-18 08:13:26.901000

Post a Comment for "Need Work-around For Handling Timestamps In Dataframe And Get Datetime"