Why a date assignments in a dataframe is not a date type?





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







3















The df_vol DataFrame is created as follows



df_vol = df.loc[:, 1].map(fd.retrieve_symbol_datetime).to_frame('maturity')
df_vol['date'] = df_vol.index.date

df_vol.head()
maturity date
2018-11-01 11:31:53.023 2022-04-01 2018-11-01
2018-11-01 16:30:15.287 2022-04-01 2018-11-01
2018-11-01 10:23:06.779 2022-10-01 2018-11-01
2018-11-01 16:30:15.291 2022-10-01 2018-11-01
2018-11-01 11:30:56.251 2018-12-01 2018-11-01


A further inspection of df_vol shows



df_vol.dtypes
maturity category
date object
dtype: object


I would expect that maturity column is of a date type as it is filled by the content of the fd.retrieve_symbol_datetime(), a function that returns pandas.datetime().
Also, the date column is an object type though it takes the values from index.date.



I'm interested in having types of datetime since I eventually I want to do the difference



pd.eval("(df_vol.maturity - df_vol.date)")


retrieve_symbol_datetime()



def retrieve_symbol_datetime(future: str):
"""
Retrieves the maturity date of a future whose format is of the form AAAMYY.

Params
-------
future : string, of form 'AAAMYY'
This format is for futures where 'AAA' is the string that identifies
the symbol, 'M' is the character that identifies the month, and 'YY' is
a two-digit number that identifies the year.

Returns : pandas.datetime
Returns the date of maturiry of the future's symbol.

Example
-------
If future = 'DI1Z20', then it returnts a pandas.datetime(2020, 12, 01).

"""
year = 2000 + int(future[4: 6])
month = convert_letter_symbol_month(future[3: 4])
return pd.datetime(year, month, 1).date()









share|improve this question




















  • 1





    It would be helpful to know what package "fd" corresponds to. Please include the relevant import statements. It also appears that you are doing df.head() which is displaying the contents of the dataframe df, but you are calculating the dataframe df_vol. Did you want to do df_vol.head() instead?

    – Tom Johnson
    Nov 24 '18 at 17:11


















3















The df_vol DataFrame is created as follows



df_vol = df.loc[:, 1].map(fd.retrieve_symbol_datetime).to_frame('maturity')
df_vol['date'] = df_vol.index.date

df_vol.head()
maturity date
2018-11-01 11:31:53.023 2022-04-01 2018-11-01
2018-11-01 16:30:15.287 2022-04-01 2018-11-01
2018-11-01 10:23:06.779 2022-10-01 2018-11-01
2018-11-01 16:30:15.291 2022-10-01 2018-11-01
2018-11-01 11:30:56.251 2018-12-01 2018-11-01


A further inspection of df_vol shows



df_vol.dtypes
maturity category
date object
dtype: object


I would expect that maturity column is of a date type as it is filled by the content of the fd.retrieve_symbol_datetime(), a function that returns pandas.datetime().
Also, the date column is an object type though it takes the values from index.date.



I'm interested in having types of datetime since I eventually I want to do the difference



pd.eval("(df_vol.maturity - df_vol.date)")


retrieve_symbol_datetime()



def retrieve_symbol_datetime(future: str):
"""
Retrieves the maturity date of a future whose format is of the form AAAMYY.

Params
-------
future : string, of form 'AAAMYY'
This format is for futures where 'AAA' is the string that identifies
the symbol, 'M' is the character that identifies the month, and 'YY' is
a two-digit number that identifies the year.

Returns : pandas.datetime
Returns the date of maturiry of the future's symbol.

Example
-------
If future = 'DI1Z20', then it returnts a pandas.datetime(2020, 12, 01).

"""
year = 2000 + int(future[4: 6])
month = convert_letter_symbol_month(future[3: 4])
return pd.datetime(year, month, 1).date()









share|improve this question




















  • 1





    It would be helpful to know what package "fd" corresponds to. Please include the relevant import statements. It also appears that you are doing df.head() which is displaying the contents of the dataframe df, but you are calculating the dataframe df_vol. Did you want to do df_vol.head() instead?

    – Tom Johnson
    Nov 24 '18 at 17:11














3












3








3








The df_vol DataFrame is created as follows



df_vol = df.loc[:, 1].map(fd.retrieve_symbol_datetime).to_frame('maturity')
df_vol['date'] = df_vol.index.date

df_vol.head()
maturity date
2018-11-01 11:31:53.023 2022-04-01 2018-11-01
2018-11-01 16:30:15.287 2022-04-01 2018-11-01
2018-11-01 10:23:06.779 2022-10-01 2018-11-01
2018-11-01 16:30:15.291 2022-10-01 2018-11-01
2018-11-01 11:30:56.251 2018-12-01 2018-11-01


A further inspection of df_vol shows



df_vol.dtypes
maturity category
date object
dtype: object


I would expect that maturity column is of a date type as it is filled by the content of the fd.retrieve_symbol_datetime(), a function that returns pandas.datetime().
Also, the date column is an object type though it takes the values from index.date.



I'm interested in having types of datetime since I eventually I want to do the difference



pd.eval("(df_vol.maturity - df_vol.date)")


retrieve_symbol_datetime()



def retrieve_symbol_datetime(future: str):
"""
Retrieves the maturity date of a future whose format is of the form AAAMYY.

Params
-------
future : string, of form 'AAAMYY'
This format is for futures where 'AAA' is the string that identifies
the symbol, 'M' is the character that identifies the month, and 'YY' is
a two-digit number that identifies the year.

Returns : pandas.datetime
Returns the date of maturiry of the future's symbol.

Example
-------
If future = 'DI1Z20', then it returnts a pandas.datetime(2020, 12, 01).

"""
year = 2000 + int(future[4: 6])
month = convert_letter_symbol_month(future[3: 4])
return pd.datetime(year, month, 1).date()









share|improve this question
















The df_vol DataFrame is created as follows



df_vol = df.loc[:, 1].map(fd.retrieve_symbol_datetime).to_frame('maturity')
df_vol['date'] = df_vol.index.date

df_vol.head()
maturity date
2018-11-01 11:31:53.023 2022-04-01 2018-11-01
2018-11-01 16:30:15.287 2022-04-01 2018-11-01
2018-11-01 10:23:06.779 2022-10-01 2018-11-01
2018-11-01 16:30:15.291 2022-10-01 2018-11-01
2018-11-01 11:30:56.251 2018-12-01 2018-11-01


A further inspection of df_vol shows



df_vol.dtypes
maturity category
date object
dtype: object


I would expect that maturity column is of a date type as it is filled by the content of the fd.retrieve_symbol_datetime(), a function that returns pandas.datetime().
Also, the date column is an object type though it takes the values from index.date.



I'm interested in having types of datetime since I eventually I want to do the difference



pd.eval("(df_vol.maturity - df_vol.date)")


retrieve_symbol_datetime()



def retrieve_symbol_datetime(future: str):
"""
Retrieves the maturity date of a future whose format is of the form AAAMYY.

Params
-------
future : string, of form 'AAAMYY'
This format is for futures where 'AAA' is the string that identifies
the symbol, 'M' is the character that identifies the month, and 'YY' is
a two-digit number that identifies the year.

Returns : pandas.datetime
Returns the date of maturiry of the future's symbol.

Example
-------
If future = 'DI1Z20', then it returnts a pandas.datetime(2020, 12, 01).

"""
year = 2000 + int(future[4: 6])
month = convert_letter_symbol_month(future[3: 4])
return pd.datetime(year, month, 1).date()






python pandas date






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 24 '18 at 17:35







user3889486

















asked Nov 24 '18 at 16:50









user3889486user3889486

3121314




3121314








  • 1





    It would be helpful to know what package "fd" corresponds to. Please include the relevant import statements. It also appears that you are doing df.head() which is displaying the contents of the dataframe df, but you are calculating the dataframe df_vol. Did you want to do df_vol.head() instead?

    – Tom Johnson
    Nov 24 '18 at 17:11














  • 1





    It would be helpful to know what package "fd" corresponds to. Please include the relevant import statements. It also appears that you are doing df.head() which is displaying the contents of the dataframe df, but you are calculating the dataframe df_vol. Did you want to do df_vol.head() instead?

    – Tom Johnson
    Nov 24 '18 at 17:11








1




1





It would be helpful to know what package "fd" corresponds to. Please include the relevant import statements. It also appears that you are doing df.head() which is displaying the contents of the dataframe df, but you are calculating the dataframe df_vol. Did you want to do df_vol.head() instead?

– Tom Johnson
Nov 24 '18 at 17:11





It would be helpful to know what package "fd" corresponds to. Please include the relevant import statements. It also appears that you are doing df.head() which is displaying the contents of the dataframe df, but you are calculating the dataframe df_vol. Did you want to do df_vol.head() instead?

– Tom Johnson
Nov 24 '18 at 17:11












1 Answer
1






active

oldest

votes


















1














There is problem categorical column, one possible solution is decategorical it and for date use floor for remove times:



df_vol['maturity'] = pd.to_datetime(df_vol['maturity'].astype(str))
df_vol['date'] = df_vol.index.floor('d')

df_vol['diff'] = (df_vol['maturity'] - df_vol['date']).dt.days
print (df_vol)
maturity date diff
2018-11-01 11:31:53.023 2022-04-01 2018-11-01 1247
2018-11-01 16:30:15.287 2022-04-01 2018-11-01 1247
2018-11-01 10:23:06.779 2022-10-01 2018-11-01 1430
2018-11-01 16:30:15.291 2022-10-01 2018-11-01 1430
2018-11-01 11:30:56.251 2018-12-01 2018-11-01 30





share|improve this answer
























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53460346%2fwhy-a-date-assignments-in-a-dataframe-is-not-a-date-type%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    There is problem categorical column, one possible solution is decategorical it and for date use floor for remove times:



    df_vol['maturity'] = pd.to_datetime(df_vol['maturity'].astype(str))
    df_vol['date'] = df_vol.index.floor('d')

    df_vol['diff'] = (df_vol['maturity'] - df_vol['date']).dt.days
    print (df_vol)
    maturity date diff
    2018-11-01 11:31:53.023 2022-04-01 2018-11-01 1247
    2018-11-01 16:30:15.287 2022-04-01 2018-11-01 1247
    2018-11-01 10:23:06.779 2022-10-01 2018-11-01 1430
    2018-11-01 16:30:15.291 2022-10-01 2018-11-01 1430
    2018-11-01 11:30:56.251 2018-12-01 2018-11-01 30





    share|improve this answer




























      1














      There is problem categorical column, one possible solution is decategorical it and for date use floor for remove times:



      df_vol['maturity'] = pd.to_datetime(df_vol['maturity'].astype(str))
      df_vol['date'] = df_vol.index.floor('d')

      df_vol['diff'] = (df_vol['maturity'] - df_vol['date']).dt.days
      print (df_vol)
      maturity date diff
      2018-11-01 11:31:53.023 2022-04-01 2018-11-01 1247
      2018-11-01 16:30:15.287 2022-04-01 2018-11-01 1247
      2018-11-01 10:23:06.779 2022-10-01 2018-11-01 1430
      2018-11-01 16:30:15.291 2022-10-01 2018-11-01 1430
      2018-11-01 11:30:56.251 2018-12-01 2018-11-01 30





      share|improve this answer


























        1












        1








        1







        There is problem categorical column, one possible solution is decategorical it and for date use floor for remove times:



        df_vol['maturity'] = pd.to_datetime(df_vol['maturity'].astype(str))
        df_vol['date'] = df_vol.index.floor('d')

        df_vol['diff'] = (df_vol['maturity'] - df_vol['date']).dt.days
        print (df_vol)
        maturity date diff
        2018-11-01 11:31:53.023 2022-04-01 2018-11-01 1247
        2018-11-01 16:30:15.287 2022-04-01 2018-11-01 1247
        2018-11-01 10:23:06.779 2022-10-01 2018-11-01 1430
        2018-11-01 16:30:15.291 2022-10-01 2018-11-01 1430
        2018-11-01 11:30:56.251 2018-12-01 2018-11-01 30





        share|improve this answer













        There is problem categorical column, one possible solution is decategorical it and for date use floor for remove times:



        df_vol['maturity'] = pd.to_datetime(df_vol['maturity'].astype(str))
        df_vol['date'] = df_vol.index.floor('d')

        df_vol['diff'] = (df_vol['maturity'] - df_vol['date']).dt.days
        print (df_vol)
        maturity date diff
        2018-11-01 11:31:53.023 2022-04-01 2018-11-01 1247
        2018-11-01 16:30:15.287 2022-04-01 2018-11-01 1247
        2018-11-01 10:23:06.779 2022-10-01 2018-11-01 1430
        2018-11-01 16:30:15.291 2022-10-01 2018-11-01 1430
        2018-11-01 11:30:56.251 2018-12-01 2018-11-01 30






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 24 '18 at 17:14









        jezraeljezrael

        361k26327407




        361k26327407
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53460346%2fwhy-a-date-assignments-in-a-dataframe-is-not-a-date-type%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            這個網誌中的熱門文章

            Xamarin.form Move up view when keyboard appear

            Post-Redirect-Get with Spring WebFlux and Thymeleaf

            Anylogic : not able to use stopDelay()