Splitting timestamp column into seperate date and time columns












16















I have a pandas dataframe with over 1000 timestamps (below) that I would like to loop through:



2016-02-22 14:59:44.561776


I'm having a hard time splitting this time stamp into 2 columns- 'date' and 'time'. The date format can stay the same, but the time needs to be converted to CST (including milliseconds).



Thanks for the help










share|improve this question




















  • 1





    How about '2016-02-22 14:59:44.561776'.split()?

    – pp_
    Feb 24 '16 at 7:25








  • 1





    Why on earth would you want to do that?!?

    – Alexander
    Feb 24 '16 at 7:27
















16















I have a pandas dataframe with over 1000 timestamps (below) that I would like to loop through:



2016-02-22 14:59:44.561776


I'm having a hard time splitting this time stamp into 2 columns- 'date' and 'time'. The date format can stay the same, but the time needs to be converted to CST (including milliseconds).



Thanks for the help










share|improve this question




















  • 1





    How about '2016-02-22 14:59:44.561776'.split()?

    – pp_
    Feb 24 '16 at 7:25








  • 1





    Why on earth would you want to do that?!?

    – Alexander
    Feb 24 '16 at 7:27














16












16








16


4






I have a pandas dataframe with over 1000 timestamps (below) that I would like to loop through:



2016-02-22 14:59:44.561776


I'm having a hard time splitting this time stamp into 2 columns- 'date' and 'time'. The date format can stay the same, but the time needs to be converted to CST (including milliseconds).



Thanks for the help










share|improve this question
















I have a pandas dataframe with over 1000 timestamps (below) that I would like to loop through:



2016-02-22 14:59:44.561776


I'm having a hard time splitting this time stamp into 2 columns- 'date' and 'time'. The date format can stay the same, but the time needs to be converted to CST (including milliseconds).



Thanks for the help







python pandas






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Feb 24 '16 at 7:24









wrkyle

362522




362522










asked Feb 24 '16 at 7:21









TomTom

110118




110118








  • 1





    How about '2016-02-22 14:59:44.561776'.split()?

    – pp_
    Feb 24 '16 at 7:25








  • 1





    Why on earth would you want to do that?!?

    – Alexander
    Feb 24 '16 at 7:27














  • 1





    How about '2016-02-22 14:59:44.561776'.split()?

    – pp_
    Feb 24 '16 at 7:25








  • 1





    Why on earth would you want to do that?!?

    – Alexander
    Feb 24 '16 at 7:27








1




1





How about '2016-02-22 14:59:44.561776'.split()?

– pp_
Feb 24 '16 at 7:25







How about '2016-02-22 14:59:44.561776'.split()?

– pp_
Feb 24 '16 at 7:25






1




1





Why on earth would you want to do that?!?

– Alexander
Feb 24 '16 at 7:27





Why on earth would you want to do that?!?

– Alexander
Feb 24 '16 at 7:27












7 Answers
7






active

oldest

votes


















15














I'm not sure why you would want to do this in the first place, but if you really must...



df = pd.DataFrame({'my_timestamp': pd.date_range('2016-1-1 15:00', periods=5)})

>>> df
my_timestamp
0 2016-01-01 15:00:00
1 2016-01-02 15:00:00
2 2016-01-03 15:00:00
3 2016-01-04 15:00:00
4 2016-01-05 15:00:00

df['new_date'] = [d.date() for d in df['my_timestamp']]
df['new_time'] = [d.time() for d in df['my_timestamp']]

>>> df
my_timestamp new_date new_time
0 2016-01-01 15:00:00 2016-01-01 15:00:00
1 2016-01-02 15:00:00 2016-01-02 15:00:00
2 2016-01-03 15:00:00 2016-01-03 15:00:00
3 2016-01-04 15:00:00 2016-01-04 15:00:00
4 2016-01-05 15:00:00 2016-01-05 15:00:00


The conversion to CST is more tricky. I assume that the current timestamps are 'unaware', i.e. they do not have a timezone attached? If not, how would you expect to convert them?



For more details:



https://docs.python.org/2/library/datetime.html



How to make an unaware datetime timezone aware in python



EDIT



An alternative method that only loops once across the timestamps instead of twice:



new_dates, new_times = zip(*[(d.date(), d.time()) for d in df['my_timestamp']])
df = df.assign(new_date=new_dates, new_time=new_times)





share|improve this answer


























  • This is what I'm looking for. I'm getting this error right now though: AttributeError: 'str' object has no attribute 'date'. I need to get these separated because I'm trying to pass the values to another script that I did not write- and this is the format he used.

    – Tom
    Feb 24 '16 at 8:18











  • That means your data is formatted as strings instead of timestamps. Try pd.to_datetime(df.my_timestamp) to see if that converts it correctly.

    – Alexander
    Feb 24 '16 at 8:21











  • Same error as above. However, when I tried one of the other suggestions from another user I got a 'Series' error in place of the str. Why would that be?

    – Tom
    Feb 24 '16 at 8:31













  • To see exactly what the string looks like, can you please post the result of df.my_timestamp.iat[0]? This will return the contents of your first timestamp. df is obviously the name of your dataframe, and my_timestamp is the name of your date column.

    – Alexander
    Feb 24 '16 at 8:36













  • 2016-02-21 21:19:44.797907

    – Tom
    Feb 24 '16 at 8:39





















10














I think the most easiest way is to use dt attribute of pandas Series. For your case you need to use dt.date and dt.time:



df = pd.DataFrame({'full_date': pd.date_range('2016-1-1 10:00:00.123', periods=10, freq='5H')})
df['date'] = df['full_date'].dt.date
df['time'] = df['full_date'].dt.time

In [166]: df
Out[166]:
full_date date time
0 2016-01-01 10:00:00.123 2016-01-01 10:00:00.123000
1 2016-01-01 15:00:00.123 2016-01-01 15:00:00.123000
2 2016-01-01 20:00:00.123 2016-01-01 20:00:00.123000
3 2016-01-02 01:00:00.123 2016-01-02 01:00:00.123000
4 2016-01-02 06:00:00.123 2016-01-02 06:00:00.123000
5 2016-01-02 11:00:00.123 2016-01-02 11:00:00.123000
6 2016-01-02 16:00:00.123 2016-01-02 16:00:00.123000
7 2016-01-02 21:00:00.123 2016-01-02 21:00:00.123000
8 2016-01-03 02:00:00.123 2016-01-03 02:00:00.123000
9 2016-01-03 07:00:00.123 2016-01-03 07:00:00.123000





share|improve this answer





















  • 3





    One caveat with the above (albeit simple and elegant) solution is that the new date column is now of type 'object' rather than datetime64. df['full_date'].dt.normalize() retains the datetime64 format, which can be helpful.

    – Sean_Calgary
    Mar 15 '18 at 16:34











  • This should be the accepted answer.

    – user32185
    Nov 14 '18 at 19:00



















5














Had same problem and this worked for me.



Suppose the date column in your dataset is called "date"



import pandas as pd
df = pd.read_csv(file_path)

df['Dates'] = pd.to_datetime(df['date']).dt.date
df['Time'] = pd.to_datetime(df['date']).dt.time


This will give you two columns "Dates" and "Time" with splited dates.






share|improve this answer































    2














    If your timestamps are already in pandas format (not string), then:



    df["date"] = df["timestamp"].date
    dt["time"] = dt["timestamp"].time


    If your timestamp is a string, you can parse it using the datetime module:



    from datetime import datetime
    data1["timestamp"] = df["timestamp"].apply(lambda x:
    datetime.strptime(x,"%Y-%m-%d %H:%M:%S.%f"))


    Source:
    http://pandas.pydata.org/pandas-docs/stable/timeseries.html






    share|improve this answer































      2














      If your timestamp is a string, you can convert it to a datetime object:



      from datetime import datetime

      timestamp = '2016-02-22 14:59:44.561776'
      dt = datetime.strptime(timestamp, '%Y-%m-%d %H:%M:%S.%f')


      From then on you can bring it to whatever format you like.






      share|improve this answer

































        0














        Try



        s = '2016-02-22 14:59:44.561776'

        date,time = s.split()


        then convert time as needed.



        If you want to further split the time,



        hour, minute, second = time.split(':')





        share|improve this answer































          0














          try this:



          def time_date(datetime_obj):
          date_time = datetime_obj.split(' ')
          time = date_time[1].split('.')
          return date_time[0], time[0]





          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%2f35595710%2fsplitting-timestamp-column-into-seperate-date-and-time-columns%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            7 Answers
            7






            active

            oldest

            votes








            7 Answers
            7






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            15














            I'm not sure why you would want to do this in the first place, but if you really must...



            df = pd.DataFrame({'my_timestamp': pd.date_range('2016-1-1 15:00', periods=5)})

            >>> df
            my_timestamp
            0 2016-01-01 15:00:00
            1 2016-01-02 15:00:00
            2 2016-01-03 15:00:00
            3 2016-01-04 15:00:00
            4 2016-01-05 15:00:00

            df['new_date'] = [d.date() for d in df['my_timestamp']]
            df['new_time'] = [d.time() for d in df['my_timestamp']]

            >>> df
            my_timestamp new_date new_time
            0 2016-01-01 15:00:00 2016-01-01 15:00:00
            1 2016-01-02 15:00:00 2016-01-02 15:00:00
            2 2016-01-03 15:00:00 2016-01-03 15:00:00
            3 2016-01-04 15:00:00 2016-01-04 15:00:00
            4 2016-01-05 15:00:00 2016-01-05 15:00:00


            The conversion to CST is more tricky. I assume that the current timestamps are 'unaware', i.e. they do not have a timezone attached? If not, how would you expect to convert them?



            For more details:



            https://docs.python.org/2/library/datetime.html



            How to make an unaware datetime timezone aware in python



            EDIT



            An alternative method that only loops once across the timestamps instead of twice:



            new_dates, new_times = zip(*[(d.date(), d.time()) for d in df['my_timestamp']])
            df = df.assign(new_date=new_dates, new_time=new_times)





            share|improve this answer


























            • This is what I'm looking for. I'm getting this error right now though: AttributeError: 'str' object has no attribute 'date'. I need to get these separated because I'm trying to pass the values to another script that I did not write- and this is the format he used.

              – Tom
              Feb 24 '16 at 8:18











            • That means your data is formatted as strings instead of timestamps. Try pd.to_datetime(df.my_timestamp) to see if that converts it correctly.

              – Alexander
              Feb 24 '16 at 8:21











            • Same error as above. However, when I tried one of the other suggestions from another user I got a 'Series' error in place of the str. Why would that be?

              – Tom
              Feb 24 '16 at 8:31













            • To see exactly what the string looks like, can you please post the result of df.my_timestamp.iat[0]? This will return the contents of your first timestamp. df is obviously the name of your dataframe, and my_timestamp is the name of your date column.

              – Alexander
              Feb 24 '16 at 8:36













            • 2016-02-21 21:19:44.797907

              – Tom
              Feb 24 '16 at 8:39


















            15














            I'm not sure why you would want to do this in the first place, but if you really must...



            df = pd.DataFrame({'my_timestamp': pd.date_range('2016-1-1 15:00', periods=5)})

            >>> df
            my_timestamp
            0 2016-01-01 15:00:00
            1 2016-01-02 15:00:00
            2 2016-01-03 15:00:00
            3 2016-01-04 15:00:00
            4 2016-01-05 15:00:00

            df['new_date'] = [d.date() for d in df['my_timestamp']]
            df['new_time'] = [d.time() for d in df['my_timestamp']]

            >>> df
            my_timestamp new_date new_time
            0 2016-01-01 15:00:00 2016-01-01 15:00:00
            1 2016-01-02 15:00:00 2016-01-02 15:00:00
            2 2016-01-03 15:00:00 2016-01-03 15:00:00
            3 2016-01-04 15:00:00 2016-01-04 15:00:00
            4 2016-01-05 15:00:00 2016-01-05 15:00:00


            The conversion to CST is more tricky. I assume that the current timestamps are 'unaware', i.e. they do not have a timezone attached? If not, how would you expect to convert them?



            For more details:



            https://docs.python.org/2/library/datetime.html



            How to make an unaware datetime timezone aware in python



            EDIT



            An alternative method that only loops once across the timestamps instead of twice:



            new_dates, new_times = zip(*[(d.date(), d.time()) for d in df['my_timestamp']])
            df = df.assign(new_date=new_dates, new_time=new_times)





            share|improve this answer


























            • This is what I'm looking for. I'm getting this error right now though: AttributeError: 'str' object has no attribute 'date'. I need to get these separated because I'm trying to pass the values to another script that I did not write- and this is the format he used.

              – Tom
              Feb 24 '16 at 8:18











            • That means your data is formatted as strings instead of timestamps. Try pd.to_datetime(df.my_timestamp) to see if that converts it correctly.

              – Alexander
              Feb 24 '16 at 8:21











            • Same error as above. However, when I tried one of the other suggestions from another user I got a 'Series' error in place of the str. Why would that be?

              – Tom
              Feb 24 '16 at 8:31













            • To see exactly what the string looks like, can you please post the result of df.my_timestamp.iat[0]? This will return the contents of your first timestamp. df is obviously the name of your dataframe, and my_timestamp is the name of your date column.

              – Alexander
              Feb 24 '16 at 8:36













            • 2016-02-21 21:19:44.797907

              – Tom
              Feb 24 '16 at 8:39
















            15












            15








            15







            I'm not sure why you would want to do this in the first place, but if you really must...



            df = pd.DataFrame({'my_timestamp': pd.date_range('2016-1-1 15:00', periods=5)})

            >>> df
            my_timestamp
            0 2016-01-01 15:00:00
            1 2016-01-02 15:00:00
            2 2016-01-03 15:00:00
            3 2016-01-04 15:00:00
            4 2016-01-05 15:00:00

            df['new_date'] = [d.date() for d in df['my_timestamp']]
            df['new_time'] = [d.time() for d in df['my_timestamp']]

            >>> df
            my_timestamp new_date new_time
            0 2016-01-01 15:00:00 2016-01-01 15:00:00
            1 2016-01-02 15:00:00 2016-01-02 15:00:00
            2 2016-01-03 15:00:00 2016-01-03 15:00:00
            3 2016-01-04 15:00:00 2016-01-04 15:00:00
            4 2016-01-05 15:00:00 2016-01-05 15:00:00


            The conversion to CST is more tricky. I assume that the current timestamps are 'unaware', i.e. they do not have a timezone attached? If not, how would you expect to convert them?



            For more details:



            https://docs.python.org/2/library/datetime.html



            How to make an unaware datetime timezone aware in python



            EDIT



            An alternative method that only loops once across the timestamps instead of twice:



            new_dates, new_times = zip(*[(d.date(), d.time()) for d in df['my_timestamp']])
            df = df.assign(new_date=new_dates, new_time=new_times)





            share|improve this answer















            I'm not sure why you would want to do this in the first place, but if you really must...



            df = pd.DataFrame({'my_timestamp': pd.date_range('2016-1-1 15:00', periods=5)})

            >>> df
            my_timestamp
            0 2016-01-01 15:00:00
            1 2016-01-02 15:00:00
            2 2016-01-03 15:00:00
            3 2016-01-04 15:00:00
            4 2016-01-05 15:00:00

            df['new_date'] = [d.date() for d in df['my_timestamp']]
            df['new_time'] = [d.time() for d in df['my_timestamp']]

            >>> df
            my_timestamp new_date new_time
            0 2016-01-01 15:00:00 2016-01-01 15:00:00
            1 2016-01-02 15:00:00 2016-01-02 15:00:00
            2 2016-01-03 15:00:00 2016-01-03 15:00:00
            3 2016-01-04 15:00:00 2016-01-04 15:00:00
            4 2016-01-05 15:00:00 2016-01-05 15:00:00


            The conversion to CST is more tricky. I assume that the current timestamps are 'unaware', i.e. they do not have a timezone attached? If not, how would you expect to convert them?



            For more details:



            https://docs.python.org/2/library/datetime.html



            How to make an unaware datetime timezone aware in python



            EDIT



            An alternative method that only loops once across the timestamps instead of twice:



            new_dates, new_times = zip(*[(d.date(), d.time()) for d in df['my_timestamp']])
            df = df.assign(new_date=new_dates, new_time=new_times)






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Feb 2 '18 at 23:44

























            answered Feb 24 '16 at 7:33









            AlexanderAlexander

            55.2k1489124




            55.2k1489124













            • This is what I'm looking for. I'm getting this error right now though: AttributeError: 'str' object has no attribute 'date'. I need to get these separated because I'm trying to pass the values to another script that I did not write- and this is the format he used.

              – Tom
              Feb 24 '16 at 8:18











            • That means your data is formatted as strings instead of timestamps. Try pd.to_datetime(df.my_timestamp) to see if that converts it correctly.

              – Alexander
              Feb 24 '16 at 8:21











            • Same error as above. However, when I tried one of the other suggestions from another user I got a 'Series' error in place of the str. Why would that be?

              – Tom
              Feb 24 '16 at 8:31













            • To see exactly what the string looks like, can you please post the result of df.my_timestamp.iat[0]? This will return the contents of your first timestamp. df is obviously the name of your dataframe, and my_timestamp is the name of your date column.

              – Alexander
              Feb 24 '16 at 8:36













            • 2016-02-21 21:19:44.797907

              – Tom
              Feb 24 '16 at 8:39





















            • This is what I'm looking for. I'm getting this error right now though: AttributeError: 'str' object has no attribute 'date'. I need to get these separated because I'm trying to pass the values to another script that I did not write- and this is the format he used.

              – Tom
              Feb 24 '16 at 8:18











            • That means your data is formatted as strings instead of timestamps. Try pd.to_datetime(df.my_timestamp) to see if that converts it correctly.

              – Alexander
              Feb 24 '16 at 8:21











            • Same error as above. However, when I tried one of the other suggestions from another user I got a 'Series' error in place of the str. Why would that be?

              – Tom
              Feb 24 '16 at 8:31













            • To see exactly what the string looks like, can you please post the result of df.my_timestamp.iat[0]? This will return the contents of your first timestamp. df is obviously the name of your dataframe, and my_timestamp is the name of your date column.

              – Alexander
              Feb 24 '16 at 8:36













            • 2016-02-21 21:19:44.797907

              – Tom
              Feb 24 '16 at 8:39



















            This is what I'm looking for. I'm getting this error right now though: AttributeError: 'str' object has no attribute 'date'. I need to get these separated because I'm trying to pass the values to another script that I did not write- and this is the format he used.

            – Tom
            Feb 24 '16 at 8:18





            This is what I'm looking for. I'm getting this error right now though: AttributeError: 'str' object has no attribute 'date'. I need to get these separated because I'm trying to pass the values to another script that I did not write- and this is the format he used.

            – Tom
            Feb 24 '16 at 8:18













            That means your data is formatted as strings instead of timestamps. Try pd.to_datetime(df.my_timestamp) to see if that converts it correctly.

            – Alexander
            Feb 24 '16 at 8:21





            That means your data is formatted as strings instead of timestamps. Try pd.to_datetime(df.my_timestamp) to see if that converts it correctly.

            – Alexander
            Feb 24 '16 at 8:21













            Same error as above. However, when I tried one of the other suggestions from another user I got a 'Series' error in place of the str. Why would that be?

            – Tom
            Feb 24 '16 at 8:31







            Same error as above. However, when I tried one of the other suggestions from another user I got a 'Series' error in place of the str. Why would that be?

            – Tom
            Feb 24 '16 at 8:31















            To see exactly what the string looks like, can you please post the result of df.my_timestamp.iat[0]? This will return the contents of your first timestamp. df is obviously the name of your dataframe, and my_timestamp is the name of your date column.

            – Alexander
            Feb 24 '16 at 8:36







            To see exactly what the string looks like, can you please post the result of df.my_timestamp.iat[0]? This will return the contents of your first timestamp. df is obviously the name of your dataframe, and my_timestamp is the name of your date column.

            – Alexander
            Feb 24 '16 at 8:36















            2016-02-21 21:19:44.797907

            – Tom
            Feb 24 '16 at 8:39







            2016-02-21 21:19:44.797907

            – Tom
            Feb 24 '16 at 8:39















            10














            I think the most easiest way is to use dt attribute of pandas Series. For your case you need to use dt.date and dt.time:



            df = pd.DataFrame({'full_date': pd.date_range('2016-1-1 10:00:00.123', periods=10, freq='5H')})
            df['date'] = df['full_date'].dt.date
            df['time'] = df['full_date'].dt.time

            In [166]: df
            Out[166]:
            full_date date time
            0 2016-01-01 10:00:00.123 2016-01-01 10:00:00.123000
            1 2016-01-01 15:00:00.123 2016-01-01 15:00:00.123000
            2 2016-01-01 20:00:00.123 2016-01-01 20:00:00.123000
            3 2016-01-02 01:00:00.123 2016-01-02 01:00:00.123000
            4 2016-01-02 06:00:00.123 2016-01-02 06:00:00.123000
            5 2016-01-02 11:00:00.123 2016-01-02 11:00:00.123000
            6 2016-01-02 16:00:00.123 2016-01-02 16:00:00.123000
            7 2016-01-02 21:00:00.123 2016-01-02 21:00:00.123000
            8 2016-01-03 02:00:00.123 2016-01-03 02:00:00.123000
            9 2016-01-03 07:00:00.123 2016-01-03 07:00:00.123000





            share|improve this answer





















            • 3





              One caveat with the above (albeit simple and elegant) solution is that the new date column is now of type 'object' rather than datetime64. df['full_date'].dt.normalize() retains the datetime64 format, which can be helpful.

              – Sean_Calgary
              Mar 15 '18 at 16:34











            • This should be the accepted answer.

              – user32185
              Nov 14 '18 at 19:00
















            10














            I think the most easiest way is to use dt attribute of pandas Series. For your case you need to use dt.date and dt.time:



            df = pd.DataFrame({'full_date': pd.date_range('2016-1-1 10:00:00.123', periods=10, freq='5H')})
            df['date'] = df['full_date'].dt.date
            df['time'] = df['full_date'].dt.time

            In [166]: df
            Out[166]:
            full_date date time
            0 2016-01-01 10:00:00.123 2016-01-01 10:00:00.123000
            1 2016-01-01 15:00:00.123 2016-01-01 15:00:00.123000
            2 2016-01-01 20:00:00.123 2016-01-01 20:00:00.123000
            3 2016-01-02 01:00:00.123 2016-01-02 01:00:00.123000
            4 2016-01-02 06:00:00.123 2016-01-02 06:00:00.123000
            5 2016-01-02 11:00:00.123 2016-01-02 11:00:00.123000
            6 2016-01-02 16:00:00.123 2016-01-02 16:00:00.123000
            7 2016-01-02 21:00:00.123 2016-01-02 21:00:00.123000
            8 2016-01-03 02:00:00.123 2016-01-03 02:00:00.123000
            9 2016-01-03 07:00:00.123 2016-01-03 07:00:00.123000





            share|improve this answer





















            • 3





              One caveat with the above (albeit simple and elegant) solution is that the new date column is now of type 'object' rather than datetime64. df['full_date'].dt.normalize() retains the datetime64 format, which can be helpful.

              – Sean_Calgary
              Mar 15 '18 at 16:34











            • This should be the accepted answer.

              – user32185
              Nov 14 '18 at 19:00














            10












            10








            10







            I think the most easiest way is to use dt attribute of pandas Series. For your case you need to use dt.date and dt.time:



            df = pd.DataFrame({'full_date': pd.date_range('2016-1-1 10:00:00.123', periods=10, freq='5H')})
            df['date'] = df['full_date'].dt.date
            df['time'] = df['full_date'].dt.time

            In [166]: df
            Out[166]:
            full_date date time
            0 2016-01-01 10:00:00.123 2016-01-01 10:00:00.123000
            1 2016-01-01 15:00:00.123 2016-01-01 15:00:00.123000
            2 2016-01-01 20:00:00.123 2016-01-01 20:00:00.123000
            3 2016-01-02 01:00:00.123 2016-01-02 01:00:00.123000
            4 2016-01-02 06:00:00.123 2016-01-02 06:00:00.123000
            5 2016-01-02 11:00:00.123 2016-01-02 11:00:00.123000
            6 2016-01-02 16:00:00.123 2016-01-02 16:00:00.123000
            7 2016-01-02 21:00:00.123 2016-01-02 21:00:00.123000
            8 2016-01-03 02:00:00.123 2016-01-03 02:00:00.123000
            9 2016-01-03 07:00:00.123 2016-01-03 07:00:00.123000





            share|improve this answer















            I think the most easiest way is to use dt attribute of pandas Series. For your case you need to use dt.date and dt.time:



            df = pd.DataFrame({'full_date': pd.date_range('2016-1-1 10:00:00.123', periods=10, freq='5H')})
            df['date'] = df['full_date'].dt.date
            df['time'] = df['full_date'].dt.time

            In [166]: df
            Out[166]:
            full_date date time
            0 2016-01-01 10:00:00.123 2016-01-01 10:00:00.123000
            1 2016-01-01 15:00:00.123 2016-01-01 15:00:00.123000
            2 2016-01-01 20:00:00.123 2016-01-01 20:00:00.123000
            3 2016-01-02 01:00:00.123 2016-01-02 01:00:00.123000
            4 2016-01-02 06:00:00.123 2016-01-02 06:00:00.123000
            5 2016-01-02 11:00:00.123 2016-01-02 11:00:00.123000
            6 2016-01-02 16:00:00.123 2016-01-02 16:00:00.123000
            7 2016-01-02 21:00:00.123 2016-01-02 21:00:00.123000
            8 2016-01-03 02:00:00.123 2016-01-03 02:00:00.123000
            9 2016-01-03 07:00:00.123 2016-01-03 07:00:00.123000






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Feb 24 '16 at 8:22

























            answered Feb 24 '16 at 7:47









            Anton ProtopopovAnton Protopopov

            15.2k34859




            15.2k34859








            • 3





              One caveat with the above (albeit simple and elegant) solution is that the new date column is now of type 'object' rather than datetime64. df['full_date'].dt.normalize() retains the datetime64 format, which can be helpful.

              – Sean_Calgary
              Mar 15 '18 at 16:34











            • This should be the accepted answer.

              – user32185
              Nov 14 '18 at 19:00














            • 3





              One caveat with the above (albeit simple and elegant) solution is that the new date column is now of type 'object' rather than datetime64. df['full_date'].dt.normalize() retains the datetime64 format, which can be helpful.

              – Sean_Calgary
              Mar 15 '18 at 16:34











            • This should be the accepted answer.

              – user32185
              Nov 14 '18 at 19:00








            3




            3





            One caveat with the above (albeit simple and elegant) solution is that the new date column is now of type 'object' rather than datetime64. df['full_date'].dt.normalize() retains the datetime64 format, which can be helpful.

            – Sean_Calgary
            Mar 15 '18 at 16:34





            One caveat with the above (albeit simple and elegant) solution is that the new date column is now of type 'object' rather than datetime64. df['full_date'].dt.normalize() retains the datetime64 format, which can be helpful.

            – Sean_Calgary
            Mar 15 '18 at 16:34













            This should be the accepted answer.

            – user32185
            Nov 14 '18 at 19:00





            This should be the accepted answer.

            – user32185
            Nov 14 '18 at 19:00











            5














            Had same problem and this worked for me.



            Suppose the date column in your dataset is called "date"



            import pandas as pd
            df = pd.read_csv(file_path)

            df['Dates'] = pd.to_datetime(df['date']).dt.date
            df['Time'] = pd.to_datetime(df['date']).dt.time


            This will give you two columns "Dates" and "Time" with splited dates.






            share|improve this answer




























              5














              Had same problem and this worked for me.



              Suppose the date column in your dataset is called "date"



              import pandas as pd
              df = pd.read_csv(file_path)

              df['Dates'] = pd.to_datetime(df['date']).dt.date
              df['Time'] = pd.to_datetime(df['date']).dt.time


              This will give you two columns "Dates" and "Time" with splited dates.






              share|improve this answer


























                5












                5








                5







                Had same problem and this worked for me.



                Suppose the date column in your dataset is called "date"



                import pandas as pd
                df = pd.read_csv(file_path)

                df['Dates'] = pd.to_datetime(df['date']).dt.date
                df['Time'] = pd.to_datetime(df['date']).dt.time


                This will give you two columns "Dates" and "Time" with splited dates.






                share|improve this answer













                Had same problem and this worked for me.



                Suppose the date column in your dataset is called "date"



                import pandas as pd
                df = pd.read_csv(file_path)

                df['Dates'] = pd.to_datetime(df['date']).dt.date
                df['Time'] = pd.to_datetime(df['date']).dt.time


                This will give you two columns "Dates" and "Time" with splited dates.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 20 '17 at 9:20









                Nodar OkroshiashviliNodar Okroshiashvili

                789616




                789616























                    2














                    If your timestamps are already in pandas format (not string), then:



                    df["date"] = df["timestamp"].date
                    dt["time"] = dt["timestamp"].time


                    If your timestamp is a string, you can parse it using the datetime module:



                    from datetime import datetime
                    data1["timestamp"] = df["timestamp"].apply(lambda x:
                    datetime.strptime(x,"%Y-%m-%d %H:%M:%S.%f"))


                    Source:
                    http://pandas.pydata.org/pandas-docs/stable/timeseries.html






                    share|improve this answer




























                      2














                      If your timestamps are already in pandas format (not string), then:



                      df["date"] = df["timestamp"].date
                      dt["time"] = dt["timestamp"].time


                      If your timestamp is a string, you can parse it using the datetime module:



                      from datetime import datetime
                      data1["timestamp"] = df["timestamp"].apply(lambda x:
                      datetime.strptime(x,"%Y-%m-%d %H:%M:%S.%f"))


                      Source:
                      http://pandas.pydata.org/pandas-docs/stable/timeseries.html






                      share|improve this answer


























                        2












                        2








                        2







                        If your timestamps are already in pandas format (not string), then:



                        df["date"] = df["timestamp"].date
                        dt["time"] = dt["timestamp"].time


                        If your timestamp is a string, you can parse it using the datetime module:



                        from datetime import datetime
                        data1["timestamp"] = df["timestamp"].apply(lambda x:
                        datetime.strptime(x,"%Y-%m-%d %H:%M:%S.%f"))


                        Source:
                        http://pandas.pydata.org/pandas-docs/stable/timeseries.html






                        share|improve this answer













                        If your timestamps are already in pandas format (not string), then:



                        df["date"] = df["timestamp"].date
                        dt["time"] = dt["timestamp"].time


                        If your timestamp is a string, you can parse it using the datetime module:



                        from datetime import datetime
                        data1["timestamp"] = df["timestamp"].apply(lambda x:
                        datetime.strptime(x,"%Y-%m-%d %H:%M:%S.%f"))


                        Source:
                        http://pandas.pydata.org/pandas-docs/stable/timeseries.html







                        share|improve this answer












                        share|improve this answer



                        share|improve this answer










                        answered Feb 24 '16 at 7:35









                        Mathieu BMathieu B

                        305211




                        305211























                            2














                            If your timestamp is a string, you can convert it to a datetime object:



                            from datetime import datetime

                            timestamp = '2016-02-22 14:59:44.561776'
                            dt = datetime.strptime(timestamp, '%Y-%m-%d %H:%M:%S.%f')


                            From then on you can bring it to whatever format you like.






                            share|improve this answer






























                              2














                              If your timestamp is a string, you can convert it to a datetime object:



                              from datetime import datetime

                              timestamp = '2016-02-22 14:59:44.561776'
                              dt = datetime.strptime(timestamp, '%Y-%m-%d %H:%M:%S.%f')


                              From then on you can bring it to whatever format you like.






                              share|improve this answer




























                                2












                                2








                                2







                                If your timestamp is a string, you can convert it to a datetime object:



                                from datetime import datetime

                                timestamp = '2016-02-22 14:59:44.561776'
                                dt = datetime.strptime(timestamp, '%Y-%m-%d %H:%M:%S.%f')


                                From then on you can bring it to whatever format you like.






                                share|improve this answer















                                If your timestamp is a string, you can convert it to a datetime object:



                                from datetime import datetime

                                timestamp = '2016-02-22 14:59:44.561776'
                                dt = datetime.strptime(timestamp, '%Y-%m-%d %H:%M:%S.%f')


                                From then on you can bring it to whatever format you like.







                                share|improve this answer














                                share|improve this answer



                                share|improve this answer








                                edited Feb 24 '16 at 18:24

























                                answered Feb 24 '16 at 7:39









                                pp_pp_

                                2,82131326




                                2,82131326























                                    0














                                    Try



                                    s = '2016-02-22 14:59:44.561776'

                                    date,time = s.split()


                                    then convert time as needed.



                                    If you want to further split the time,



                                    hour, minute, second = time.split(':')





                                    share|improve this answer




























                                      0














                                      Try



                                      s = '2016-02-22 14:59:44.561776'

                                      date,time = s.split()


                                      then convert time as needed.



                                      If you want to further split the time,



                                      hour, minute, second = time.split(':')





                                      share|improve this answer


























                                        0












                                        0








                                        0







                                        Try



                                        s = '2016-02-22 14:59:44.561776'

                                        date,time = s.split()


                                        then convert time as needed.



                                        If you want to further split the time,



                                        hour, minute, second = time.split(':')





                                        share|improve this answer













                                        Try



                                        s = '2016-02-22 14:59:44.561776'

                                        date,time = s.split()


                                        then convert time as needed.



                                        If you want to further split the time,



                                        hour, minute, second = time.split(':')






                                        share|improve this answer












                                        share|improve this answer



                                        share|improve this answer










                                        answered Feb 24 '16 at 7:27









                                        wrkylewrkyle

                                        362522




                                        362522























                                            0














                                            try this:



                                            def time_date(datetime_obj):
                                            date_time = datetime_obj.split(' ')
                                            time = date_time[1].split('.')
                                            return date_time[0], time[0]





                                            share|improve this answer






























                                              0














                                              try this:



                                              def time_date(datetime_obj):
                                              date_time = datetime_obj.split(' ')
                                              time = date_time[1].split('.')
                                              return date_time[0], time[0]





                                              share|improve this answer




























                                                0












                                                0








                                                0







                                                try this:



                                                def time_date(datetime_obj):
                                                date_time = datetime_obj.split(' ')
                                                time = date_time[1].split('.')
                                                return date_time[0], time[0]





                                                share|improve this answer















                                                try this:



                                                def time_date(datetime_obj):
                                                date_time = datetime_obj.split(' ')
                                                time = date_time[1].split('.')
                                                return date_time[0], time[0]






                                                share|improve this answer














                                                share|improve this answer



                                                share|improve this answer








                                                edited Feb 24 '16 at 9:59









                                                Fabio Lamanna

                                                8,32194987




                                                8,32194987










                                                answered Feb 24 '16 at 7:33









                                                Nitin SanghiNitin Sanghi

                                                3411




                                                3411






























                                                    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%2f35595710%2fsplitting-timestamp-column-into-seperate-date-and-time-columns%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







                                                    這個網誌中的熱門文章

                                                    Academy of Television Arts & Sciences

                                                    L'Équipe

                                                    1995 France bombings