TypeError: can only concatenate str (not "Timedelta") to str

def maximum(wc_slab, shot_times):
    max_values = []
    max_values_index = []
    
    for time in shot_times:
        window = wc_slab[time:min(time + pd.Timedelta(minutes=10), len(wc_slab))]  
        max_value = max(window)
        max_value_index = np.argmax(window) + time
        max_values.append(max_value)
        max_values_index.append(str(max_value_index))  # Convert the index to a string
    return max_values, max_values_index

max_values, max_values_index = maximum(watergifDataMerged['WC_slab1'], shot_times)

I want to retrieve the maximum value and on which time this occured of the wc_slab. And it is only allowed to search between two time points of shot_times but everytime I get this error: TypeError: can only concatenate str (not “Timedelta”) to str.
The time is notated in this way ‘18/01/2020 02:10’

I’m a basic programmer but can’t solve this problem, can anyone help me?

time + pd.Timedelta(minutes=10)

Here, time is a str while pd.Timedelta(minutes=10) returns a Timedelta. These objects cannot be added together, because str does not know what to do with a Timedelta, and Timedelta does not know what to do with a str.

I think you expected something like this

>>> '18/01/2020 02:10' + pd.Timedelta(minutes=10)  # Does not actually work.
'18/01/2020 02:20'

correct? To achieve this, convert the time string to a timestamp first

>>> ts = pd.Timestamp('18/01/2020 02:10') + pd.Timedelta(minutes=10)  # Does work.
>>> ts
Timestamp('2020-01-18 02:20:00')

which you can then convert back to a string, if that is what you need

>>> ts.strftime("%D/%m/%Y %H:%M")
'01/18/20/01/2020 02:20'

However, I wonder if that will actually do what you want. This slice

wc_slab[time : min(time + pd.Timedelta(minutes=10), len(wc_slab))]

does not look likely to work with str indices, especially since len(wc_slab) is definitely not str. What kind if thing is wc_slab, and how is it indexed?

Hi Alexander,

Thank you for your answer, very helpfull!

wc_slab is a percent of a waterContent shown as 67.10

I have a csv file where there is logged every five minutes, so in fact I want to know the highest waterContent (wc_slab) between shot_times[1] and shot_times[2] these two times can be two hourse from each other. So the function needs to itterate over all the wc_slab and that’s what I tried with len(wc_slab).

Do you have any suggestions to make this working? To connect the date and time to the highest wc_slab for every shot_times

So, wc_slab is a list[float], with values sampled five minutes apart. Since you’re already using pandas, I would associate wc_slab with a DatetimeIndex

import pandas as pd

start = pd.Timestamp('18/01/2020 02:10')
index = pd.DatetimeIndex(
    data=[
        start + i for i in (pd.Timedelta(minutes=j) for j in range(0, len(wc_slab), 5))
    ]
)
wc_slab_tsidx = pd.Series(wc_slab, index=index)

Then, you can index this Series using Timestamps

wc_slab_tsidx[start : start + pd.Timedelta(minutes=10)]

You got a good answer already, but I think it will be useful to show how to think about error messages properly.

Let’s think about the problem logically.
Do we understand what “concatenate” means? If not, we can look that up. We find that it means: to take two strings (pieces of text), and figure out the string that we get by putting them one after the other.

The next step is to apply logic. The error says that we can only use a str to concatenate to a str, and that we can’t use a Timedelta (i.e., a time-delta) to concatenate to a str (i.e., a string). Therefore, what is the problem? It must be, that the code says to concatenate a time-delta to a string, and this is a problem because it does not make sense to try that.

Where does the code say that? In the place that the error message tells us. Experienced programmers can see the problem anyway, but for your own reference you should look at the entire error message, which will tell you this. It will point to the line window = wc_slab[time:min(time + pd.Timedelta(minutes=10), len(wc_slab))]; and in newer versions it should highlight (by using ^ symbols underneath) the time + pd.TimeDelta(minutes=10) part.

Next step: did we want to concatenate to a string? I am guessing no. I am guessing that you expected to have a time and a time-delta; and that you wanted to use + as arithmetic, to find the new time.

Therefore, what is wrong? We expected to have a time and a time-delta, and the error message tells us that we have a string and a time-delta. Therefore, the problem is that we have a string where we should have a time instead.

Why do we have a string instead of a time? We answer this by checking to find out where the value came from. Here, the code says for time in shot_times:, so that’s where the time value came from. It seems like shot_times was expected to contain times, but it contains strings instead.

Should shot_times contain strings? Only you can answer this, because it is your code and you are the one who planned out the design.

If shot_times should contain times instead, then we need to fix the code that set it up in the first place. Otherwise, we need to fix the code here, so that we can take the string that was given to us, and get a time instead.

Given a string, how do we get a time? You show how the time “is notated” - this makes it sound like you knew that you had a string all along, and really just want to know how to do the conversion. In this case, it would be better to ask about that directly - both when posting on a forum, and when trying to use a search engine to get help. Most of the time, debugging an error is much harder than researching a how-to question.

That said, there are many ways to do this. Since it appears that you are already using Pandas, the natural way is to create a pd.Timestamp from the string, the way that @abessman shows. That is a type provided by Pandas that describes a time, to which a Pandas time-delta can be added.