1

I currently have a dataframe loaded from a csv as such:

   name                  week  content
0  Dan  2012-07-09/2012-07-15      4.0
1  Jim  2012-07-09/2012-07-15      1.0
2  Joe  2012-07-09/2012-07-15      3.0
3  Sam  2012-07-16/2012-07-22     18.0
4  Tom  2012-07-16/2012-07-22      7.0

The datetime period week is stored as a string. How would I go about converting this to a datetime period?

Yaakov Bressler
  • 4,437
  • 2
  • 19
  • 41

1 Answers1

3

You can convert first of datetimes by to_datetime and then use Series.dt.to_period:

df['week'] = pd.to_datetime(df['week'].str.split('/').str[0]).dt.to_period('W')

print (df)
  name                   week  content
0  Dan  2012-07-09/2012-07-15      4.0
1  Jim  2012-07-09/2012-07-15      1.0
2  Joe  2012-07-09/2012-07-15      3.0
3  Sam  2012-07-16/2012-07-22     18.0
4  Tom  2012-07-16/2012-07-22      7.0

print (df['week'])
0    2012-07-09/2012-07-15
1    2012-07-09/2012-07-15
2    2012-07-09/2012-07-15
3    2012-07-16/2012-07-22
4    2012-07-16/2012-07-22
Name: week, dtype: period[W-SUN]

If want parse values in read_csv use converters with lambda function:

import pandas as pd
from io import StringIO

temp="""name;week;content
0;Dan;2012-07-09/2012-07-15;4.0
1;Jim;2012-07-09/2012-07-15;1.0
2;Joe;2012-07-09/2012-07-15;3.0
3;Sam;2012-07-16/2012-07-22;18.0
4;Tom;2012-07-16/2012-07-22;7.0"""
#after testing replace 'pd.compat.StringIO(temp)' to 'filename.csv'

f = lambda x: pd.to_datetime(x.split('/')[0]).to_period('W')
df = pd.read_csv(StringIO(temp), sep=";", converters={'week': f})

print (df)
  name                   week  content
0  Dan  2012-07-09/2012-07-15      4.0
1  Jim  2012-07-09/2012-07-15      1.0
2  Joe  2012-07-09/2012-07-15      3.0
3  Sam  2012-07-16/2012-07-22     18.0
4  Tom  2012-07-16/2012-07-22      7.0

print (df.dtypes)
name              object
week       period[W-SUN]
content          float64
dtype: object
jezrael
  • 629,482
  • 62
  • 918
  • 895
  • 1
    the converter usage is really nice! but since this ignores the second part of the date string, would there be a way to check if the period is valid? – MrFuppes Apr 28 '20 at 12:12