0

I know I'm almost there, but can't figure out the faster syntax for the equivalent of the below code:

def is_evening(df):
    lst = []
    for i in df.index:
        val = df["localized_time"][i]
        if val != 'NA':
            if val.hour >= 17 and val.hour <= 3:
                lst.append(1)
            else:
                lst.append(0)
        else:
            lst.append(-1)
    df["is_evening"] = lst
    return df

If it was just two values it would be easy, but with the additional conditions and stuff, is there a fast way to do this using pandas map?

What I have so far:

mask = [df["localized_time"] != 'NA']
df["is_evening"] = df["is_evening"] = df["localized_time"][mask].apply(lambda x: 1 if (x.hour >= 17 and x.hour <= 23) else 0)
# Doesn't seem to work

Could anyone help me out? Thank you! :)

2
  • I can't imagine when val.hour >= 17 and val.hour <= 3 would be True ;-) Commented May 20, 2017 at 18:06
  • @MaxU Holy crap tired coding does not help, thank you for spotting that! Commented May 21, 2017 at 3:45

1 Answer 1

1

Yes there definitely is:

def f(x):
    return int(x.hour >= 17 and x.hour <= 3) if x != 'NA' else -1

df["is_evening"] = df["localized_time"].apply(f)

So here f is a function that maps a single localized_time element to an is_evening element. We can also use a lambda expression, but this will be more ugly:

df["is_evening"] = df["localized_time"].\
    apply(lambda x: int(x.hour >= 17 and x.hour <= 3) if x != 'NA' else -1)
Sign up to request clarification or add additional context in comments.

2 Comments

Awesome! Thank you so much! :)
Note that while this is more concise, it still uses Python-level functions instead of vectorized operations.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.