简体   繁体   中英

Extracting the week from python datetime and getting the serial number?

I have a dataframe with column name order date containing date from July 2014 to June 2015 in the format 2014-10-17 15:11:54. Using datetime I have extracted the week number from the date. However I get the starting week as 27 for July 2014 than for January 2015 starts again as week 1. What I want is July 2014 as week 1 continue till June 2015 and ends as 53.

df['Week'] = df.order_date.dt.week 

Use the above code to get the week number after that to get as 1 for July 2014 use

def time_period(x):
    if df.Week >= 26:
        return df.Week -25
    else:
        return df.Week +28
df['week_serial'] = df.Week.apply(lambda x: time_period(x))

This gives an error- The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

Well since df.Week already contains the week number, the function should look like:

def time_period():
    if  >= 26:
        return -25
    else:
        return +28

But I think you here basically are looking for a modulo operation:

df['week_serial'] = (df['Week'] + 27)  + 1

This will map 26 on 1 , 27 on 2 , etc.; and 25 on 53 and 24 on 52 , etc.

So for sample input:

>>> df
   Week
0    13
1    49
2    47
3    12
4    35
5    17
6     1
7    46
8    19
9     0

we obtain:

>>> (df['Week'] + 27) % 53 + 1
0    41
1    24
2    22
3    40
4    10
5    45
6    29
7    21
8    47
9    28
Name: Week, dtype: int64

Given that you already have datetime.datetime objects, it is probably the easiest to use those.

First, define your start date.

In [1]: import datetime

In [2]: start  = datetime.datetime(2014, 7, 1)
Out[2]: datetime.datetime(2014, 7, 1, 0, 0)

Then determine the timedelta between each date and the start, and convert that to days and then weeks.

In [3]: (datetime.datetime(2015, 3, 24) - start).days
Out[3]: 266

In [4]: (datetime.datetime(2015, 3, 24) - start).days // 7 + 1
Out[4]: 39

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM