Convert integer to timedelta python

I have a data frame in pandas which includes number of days since an event occurred. I want to create a new column that calculates the date of the event by subtracting the number of days from the current date. Every time I attempt to apply pd.offsets.Day or pd.Timedelta I get an error stating that Series are an unsupported type. This also occurs when I use apply. When I use map I receive a runtime error saying "maximum recursion depth exceeded while calling a Python object".

For example, assume my data frame looked like this:

index    days_since_event
0        5
1        7
2        3
3        6
4        0

I want to create a new column with the date of the event, so my expected outcome [using today's date of 12/29/2015]

index    days_since_event    event_date
0        5                   2015-12-24
1        7                   2015-12-22
2        3                   2015-12-26
3        6                   2015-12-23
4        0                   2015-12-29

I have attempted multiple ways to do this, but have received errors for each.

One method I tried was:

now = pd.datetime.date[pd.datetime.now[]]
df['event_date'] = now - df.days_since_event.apply[pd.offsets.Day]

With this I received an error saying that Series are an unsupported type.

I tried the above with .map instead of .apply, and received the error that "maximum recursion depth exceeded while calling a Python object".

I also attempted to convert the days into timedelta, such as:

df.days_since_event = [dt.timedelta[days = df.days_since_event]].apply

This also received an error referencing the series being an unsupported type.

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    Let’s discuss how to convert an Integer to Datetime in it. Now to convert Integers to Datetime in Pandas DataFrame.

    Syntax of  pd.to_datetime

    df['DataFrame Column'] = pd.to_datetime[df['DataFrame Column'], format=specify your format]

    Create the DataFrame to Convert Integer to Datetime in Pandas

    Check data type for the ‘Dates’ column is Integer.

    Python

    import pandas as pd

    values = {'Dates':  [20190902, 20190913, 20190921],

              'Attendance': ['Attended', 'Not Attended', 'Attended']

              }

    df = pd.DataFrame[values, columns=['Dates', 'Attendance']]

    print[df]

    print[df.dtypes]

    Output:

    Example 1:

    Now to convert it into Datetime we use the previously mentioned syntax. Since in this example the date format is yyyymmdd, the date format can be represented as follows:

    format= '%Y%m%d'

    Python3

    import pandas as pd

    values = {'Dates':  [20190902, 20190913, 20190921],

              'Attendance': ['Attended', 'Not Attended', 'Attended']

              }

    df = pd.DataFrame[values, columns=['Dates', 'Attendance']]

    df['Dates'] = pd.to_datetime[df['Dates'], format='%Y%m%d']

    print[df]

    print[df.dtypes]

    Output:

    Example 2: 

    Now, suppose the Pandas DataFrame has a date in the format yymmdd. In this case, the date format would now contain ‘y’ in lowercase:

    format='%y%m%d'

    Python3

    import pandas as pd

    values = {'Dates':  [190902, 190913, 190921],

              'Attendance': ['Attended', 'Not Attended', 'Attended']

              }

    df = pd.DataFrame[values, columns=['Dates', 'Attendance']]

    df['Dates'] = pd.to_datetime[df['Dates'], format='%y%m%d']

    print[df]

    print[df.dtypes]

    Output:

    Example 3: 

    Now, let’s suppose that your integers contain both date and time. In that case, the format that you should specify is:

    format='%Y%m%d%H%M%S'

    Python3

    import pandas as pd

    values = {'Dates': [20190902093000, 20190913093000, 20190921200000],

              'Attendance': ['Attended', 'Not Attended', 'Attended']

              }

    df = pd.DataFrame[values, columns=['Dates', 'Attendance']]

    df['Dates'] = pd.to_datetime[df['Dates'], format='%Y%m%d%H%M%S']

    print[df]

    print[df.dtypes]

    Output:

    Example 4: 

    Consider this DataFrame with microseconds in our DateTime values. In this case, the format should be specified as:

    format='%Y%m%d%H%M%S%F'

    Python3

    import pandas as pd

    values = {'Dates':  [20190902093000912, 20190913093000444,

                         20190921200000009],

              'Attendance': ['Attended', 'Not Attended', 'Attended']

              }

    df = pd.DataFrame[values, columns=['Dates', 'Attendance']]

    df['Dates'] = pd.to_datetime[df['Dates'], format='%Y%m%d%H%M%S%F']

    print[df]

    print[df.dtypes]

    Output:


    How do I convert Timedelta to int in Python?

    How to Convert Timedelta to Int in Pandas [With Examples].
    Method 1: Convert Timedelta to Integer [Days] df['days'] = df['timedelta_column']. ... .
    Method 2: Convert Timedelta to Integer [Hours] df['hours'] = df['timedelta_column'] / pd..

    How do I convert a number to a datetime in Python?

    Use pandas. to_datetime[] to Convert Integer to Date & Time Format. Let's suppose that your integers contain both the date and time. In that case, the format should be specify is '%Y%m%d%H%M%S' .

    How do you convert int to timestamp in python?

    “convert integer unix to timestamp python” Code Answer.
    import datetime..
    print[.
    datetime. datetime. fromtimestamp[.
    int["1284105682"].
    ]. strftime['%H:%M:%S'].

    How do you convert int to string in Python?

    In Python an integer can be converted into a string using the built-in str[] function. The str[] function takes in any python data type and converts it into a string.

    Chủ Đề