0

I have time in format : pandas._libs.tslibs.timestamps.Timestamp,i need to pick every minutes in this timeSeries

2011-08-01 00:00:14
2011-08-01 00:00:17
2011-08-01 00:00:20
2011-08-01 00:00:23
2011-08-01 00:00:26
2011-08-01 00:00:29
2011-08-01 00:00:32
2011-08-01 00:00:35
2011-08-01 00:00:38
2011-08-01 00:00:41
2011-08-01 00:00:44
2011-08-01 00:01:00
2011-08-01 00:01:03
2011-08-01 00:01:06
2011-08-01 00:01:09
2011-08-01 00:01:12
2011-08-01 00:02:03
2011-08-01 00:02:06
2011-08-01 00:01:15
...
...

so below is the code i have tried so far:

def mins():

    for i in range(len(df1['Time'])):
        labels = df1['Time'][i].strftime('%M')

        print (labels)
    return (labels)

so if i call the function, only the last minute is returned as an output, it actually shows the rest of minutes but not as an 'output' so i cannot be able to call that later, i have tried so many things (i.e appending labels in a lst) but nothing worked, may you please help people.

Lamanus
  • 12,898
  • 4
  • 21
  • 47
Mbali Aleh
  • 41
  • 6

2 Answers2

0

The labels variable gets updated in each iteration of the loop. One way to fix this is to instead append the item in each iteration instead:

labels = []
for i in range(len(df1['Time'])):
    labels.append(df1['Time'][i].strftime('%M'))
return labels

But this is quite inefficient. You can do this:

list(df1['Time'].apply(lambda x: x.strftime('%M')))

This applies the lambda function (x.strftime('%M')) to each element of the series, or even:

[x.strftime('%M') for x in list(df1['Time'])]

This first converts the series into a list, then use list comprehension to apply the function.

Art
  • 453
  • 4
  • 12
0

Art has a great concise single line solution to your problem. As he pointed out 'labels' is a single value that gets overwritten each time you cycle through your 'for' loop. You need to define a list for append() to work properly. If you wanted your code to work it should have looked more like this:

#Import the necessary modules
import datetime as dt
import pandas as pd

#Make a compatible data set to test (note I copied and pasted the first five values but then edited the minutes to make it more interesting)
time_series = ['2011-08-01 00:00:14', '2011-08-01 00:00:17', '2011-08-01 00:01:20', '2011-08-01 00:02:23', '2011-08-01 00:05:26']
dates_list = [dt.datetime.strptime(date, '%Y-%m-%d %H:%M:%S') for date in time_series]
#Convert to a dataframe
df = pd.DataFrame(dates_list, columns=['Time'])

#define a function (sorry I didn't like mins for a function name)
def get_mins(df1): #pass the dataframe to the function
    list_of_mins = [] #Initialise an empty list
    for i in range(len(df1['Time'])): 
        labels = df1['Time'][i].strftime('%M')
        list_of_mins.append(labels) # append labels to the list
        print (labels) #Probably want to delete this line
    return (list_of_mins) #Return the list

get_mins(df)

This should output:

00

00

01

02

05

['00', '00', '01', '02', '05']

Goodluck! ​

Cooper
  • 68
  • 7