Formatting a string in datetime using Pandas - problem with directives

I have a line that represents a full year, followed by an ISO week of the year (therefore, several years have 53 weeks, because weekly counting starts from the first full week of the year). I want to convert it to an object datetimeusing pandas.to_datetime(). So I:

pandas.to_datetime('201145', format='%Y%W')

and it returns:

Timestamp('2011-01-01 00:00:00')

which is wrong. Or if I try:

pandas.to_datetime('201145', format='%Y%V')

he tells me what %Vis a bad directive.

What am I doing wrong?

+4
source share
1 answer

I think the following question will be useful for you: Reverse date.isocalender ()

, , :

import datetime
import pandas as pd
def iso_year_start(iso_year):
    "The gregorian calendar date of the first day of the given ISO year"
    fourth_jan = datetime.date(iso_year, 1, 4)
    delta = datetime.timedelta(fourth_jan.isoweekday()-1)
    return fourth_jan - delta 

def iso_to_gregorian(iso_year, iso_week, iso_day):
    "Gregorian calendar date for the given ISO year, week and day"
    year_start = iso_year_start(iso_year)
    return year_start + datetime.timedelta(days=iso_day-1, weeks=iso_week-1)

def time_stamp(yourString):
    year = int(yourString[0:4])
    week = int(yourString[-2:])
    day = 1
    return year, week, day

yourTimeStamp = iso_to_gregorian( time_stamp('201145')[0] , time_stamp('201145')[1], time_stamp('201145')[2] )

print yourTimeStamp

dataframe.

, , :

2011-11-07
+2

Source: https://habr.com/ru/post/1649219/


All Articles