Extract unique monthly periods from pandas datetime column












1















I have a date column like this.



2012-01-28
2012-01-28
2012-01-28
2012-01-28
2012-01-28
2012-01-28
2012-01-28
2012-01-28
2012-01-28
2012-01-28
2012-01-28
2012-01-28
2012-01-29
2012-01-29
2012-01-29
2012-01-29
2012-01-29
2012-01-29

....
2016-12-31
2016-12-31
2016-12-31
2016-12-31


I want to convert it into any of the below format: i.e get the unique yyyy-mm



2012-01  or 2012-Jan   or Jan
2012-02 or 2012-Feb or Feb
2012-03
...
2016-12 or 2012-Dec or Dec









share|improve this question

























  • Welcome to StackOverflow. Please read and follow the posting guidelines in the help documentation, as suggested when you created this account. On topic, how to ask, and ... the perfect question apply here. StackOverflow is not a design, coding, research, or tutorial resource. However, if you follow whatever resources you find on line, make an honest coding attempt, and run into a problem, you'd have a good example to post.

    – Prune
    Dec 28 '18 at 20:16











  • If your question was answered, please vote on, and accept the most helpful answer here. You can accept an answer by clicking the grey check to the left of the answer to toggle it green. TIA.

    – coldspeed
    Jan 12 at 23:04
















1















I have a date column like this.



2012-01-28
2012-01-28
2012-01-28
2012-01-28
2012-01-28
2012-01-28
2012-01-28
2012-01-28
2012-01-28
2012-01-28
2012-01-28
2012-01-28
2012-01-29
2012-01-29
2012-01-29
2012-01-29
2012-01-29
2012-01-29

....
2016-12-31
2016-12-31
2016-12-31
2016-12-31


I want to convert it into any of the below format: i.e get the unique yyyy-mm



2012-01  or 2012-Jan   or Jan
2012-02 or 2012-Feb or Feb
2012-03
...
2016-12 or 2012-Dec or Dec









share|improve this question

























  • Welcome to StackOverflow. Please read and follow the posting guidelines in the help documentation, as suggested when you created this account. On topic, how to ask, and ... the perfect question apply here. StackOverflow is not a design, coding, research, or tutorial resource. However, if you follow whatever resources you find on line, make an honest coding attempt, and run into a problem, you'd have a good example to post.

    – Prune
    Dec 28 '18 at 20:16











  • If your question was answered, please vote on, and accept the most helpful answer here. You can accept an answer by clicking the grey check to the left of the answer to toggle it green. TIA.

    – coldspeed
    Jan 12 at 23:04














1












1








1








I have a date column like this.



2012-01-28
2012-01-28
2012-01-28
2012-01-28
2012-01-28
2012-01-28
2012-01-28
2012-01-28
2012-01-28
2012-01-28
2012-01-28
2012-01-28
2012-01-29
2012-01-29
2012-01-29
2012-01-29
2012-01-29
2012-01-29

....
2016-12-31
2016-12-31
2016-12-31
2016-12-31


I want to convert it into any of the below format: i.e get the unique yyyy-mm



2012-01  or 2012-Jan   or Jan
2012-02 or 2012-Feb or Feb
2012-03
...
2016-12 or 2012-Dec or Dec









share|improve this question
















I have a date column like this.



2012-01-28
2012-01-28
2012-01-28
2012-01-28
2012-01-28
2012-01-28
2012-01-28
2012-01-28
2012-01-28
2012-01-28
2012-01-28
2012-01-28
2012-01-29
2012-01-29
2012-01-29
2012-01-29
2012-01-29
2012-01-29

....
2016-12-31
2016-12-31
2016-12-31
2016-12-31


I want to convert it into any of the below format: i.e get the unique yyyy-mm



2012-01  or 2012-Jan   or Jan
2012-02 or 2012-Feb or Feb
2012-03
...
2016-12 or 2012-Dec or Dec






python pandas datetime






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 28 '18 at 19:45









coldspeed

124k22125208




124k22125208










asked Dec 28 '18 at 19:40









Dhiraj KumarDhiraj Kumar

434




434













  • Welcome to StackOverflow. Please read and follow the posting guidelines in the help documentation, as suggested when you created this account. On topic, how to ask, and ... the perfect question apply here. StackOverflow is not a design, coding, research, or tutorial resource. However, if you follow whatever resources you find on line, make an honest coding attempt, and run into a problem, you'd have a good example to post.

    – Prune
    Dec 28 '18 at 20:16











  • If your question was answered, please vote on, and accept the most helpful answer here. You can accept an answer by clicking the grey check to the left of the answer to toggle it green. TIA.

    – coldspeed
    Jan 12 at 23:04



















  • Welcome to StackOverflow. Please read and follow the posting guidelines in the help documentation, as suggested when you created this account. On topic, how to ask, and ... the perfect question apply here. StackOverflow is not a design, coding, research, or tutorial resource. However, if you follow whatever resources you find on line, make an honest coding attempt, and run into a problem, you'd have a good example to post.

    – Prune
    Dec 28 '18 at 20:16











  • If your question was answered, please vote on, and accept the most helpful answer here. You can accept an answer by clicking the grey check to the left of the answer to toggle it green. TIA.

    – coldspeed
    Jan 12 at 23:04

















Welcome to StackOverflow. Please read and follow the posting guidelines in the help documentation, as suggested when you created this account. On topic, how to ask, and ... the perfect question apply here. StackOverflow is not a design, coding, research, or tutorial resource. However, if you follow whatever resources you find on line, make an honest coding attempt, and run into a problem, you'd have a good example to post.

– Prune
Dec 28 '18 at 20:16





Welcome to StackOverflow. Please read and follow the posting guidelines in the help documentation, as suggested when you created this account. On topic, how to ask, and ... the perfect question apply here. StackOverflow is not a design, coding, research, or tutorial resource. However, if you follow whatever resources you find on line, make an honest coding attempt, and run into a problem, you'd have a good example to post.

– Prune
Dec 28 '18 at 20:16













If your question was answered, please vote on, and accept the most helpful answer here. You can accept an answer by clicking the grey check to the left of the answer to toggle it green. TIA.

– coldspeed
Jan 12 at 23:04





If your question was answered, please vote on, and accept the most helpful answer here. You can accept an answer by clicking the grey check to the left of the answer to toggle it green. TIA.

– coldspeed
Jan 12 at 23:04












1 Answer
1






active

oldest

votes


















2














Use DatetimeIndex.to_period:



pd.DatetimeIndex(df['date']).to_period('M').unique()
# PeriodIndex(['2012-01', '2016-12'], dtype='period[M]', name='date', freq='M')




If month names are needed, use strftime:



df['date'].dt.strftime('%Y-%b').unique()
# array(['2012-Jan', '2016-Dec'], dtype=object)


If Series format is necessary, use drop_duplicates:



df['date'].dt.strftime('%Y-%b').drop_duplicates()

0 2012-Jan
18 2016-Dec
Name: date, dtype: object





share|improve this answer
























  • @W-B Wow, did not notice such a thread... interesting indeed!

    – coldspeed
    Dec 28 '18 at 19:51






  • 1





    Just Jez, you and Pir are too good and' jealous people'

    – W-B
    Dec 28 '18 at 19:52













  • @W-B Add yourself to that list please, you are one of us and have been so for a long time.

    – coldspeed
    Dec 28 '18 at 19:53






  • 1





    Nope I only have limit years experience with python and learn from your guys :-)

    – W-B
    Dec 28 '18 at 19:54








  • 1





    Just catch an good answer there

    – W-B
    Dec 28 '18 at 20:07











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%2f53963553%2fextract-unique-monthly-periods-from-pandas-datetime-column%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









2














Use DatetimeIndex.to_period:



pd.DatetimeIndex(df['date']).to_period('M').unique()
# PeriodIndex(['2012-01', '2016-12'], dtype='period[M]', name='date', freq='M')




If month names are needed, use strftime:



df['date'].dt.strftime('%Y-%b').unique()
# array(['2012-Jan', '2016-Dec'], dtype=object)


If Series format is necessary, use drop_duplicates:



df['date'].dt.strftime('%Y-%b').drop_duplicates()

0 2012-Jan
18 2016-Dec
Name: date, dtype: object





share|improve this answer
























  • @W-B Wow, did not notice such a thread... interesting indeed!

    – coldspeed
    Dec 28 '18 at 19:51






  • 1





    Just Jez, you and Pir are too good and' jealous people'

    – W-B
    Dec 28 '18 at 19:52













  • @W-B Add yourself to that list please, you are one of us and have been so for a long time.

    – coldspeed
    Dec 28 '18 at 19:53






  • 1





    Nope I only have limit years experience with python and learn from your guys :-)

    – W-B
    Dec 28 '18 at 19:54








  • 1





    Just catch an good answer there

    – W-B
    Dec 28 '18 at 20:07
















2














Use DatetimeIndex.to_period:



pd.DatetimeIndex(df['date']).to_period('M').unique()
# PeriodIndex(['2012-01', '2016-12'], dtype='period[M]', name='date', freq='M')




If month names are needed, use strftime:



df['date'].dt.strftime('%Y-%b').unique()
# array(['2012-Jan', '2016-Dec'], dtype=object)


If Series format is necessary, use drop_duplicates:



df['date'].dt.strftime('%Y-%b').drop_duplicates()

0 2012-Jan
18 2016-Dec
Name: date, dtype: object





share|improve this answer
























  • @W-B Wow, did not notice such a thread... interesting indeed!

    – coldspeed
    Dec 28 '18 at 19:51






  • 1





    Just Jez, you and Pir are too good and' jealous people'

    – W-B
    Dec 28 '18 at 19:52













  • @W-B Add yourself to that list please, you are one of us and have been so for a long time.

    – coldspeed
    Dec 28 '18 at 19:53






  • 1





    Nope I only have limit years experience with python and learn from your guys :-)

    – W-B
    Dec 28 '18 at 19:54








  • 1





    Just catch an good answer there

    – W-B
    Dec 28 '18 at 20:07














2












2








2







Use DatetimeIndex.to_period:



pd.DatetimeIndex(df['date']).to_period('M').unique()
# PeriodIndex(['2012-01', '2016-12'], dtype='period[M]', name='date', freq='M')




If month names are needed, use strftime:



df['date'].dt.strftime('%Y-%b').unique()
# array(['2012-Jan', '2016-Dec'], dtype=object)


If Series format is necessary, use drop_duplicates:



df['date'].dt.strftime('%Y-%b').drop_duplicates()

0 2012-Jan
18 2016-Dec
Name: date, dtype: object





share|improve this answer













Use DatetimeIndex.to_period:



pd.DatetimeIndex(df['date']).to_period('M').unique()
# PeriodIndex(['2012-01', '2016-12'], dtype='period[M]', name='date', freq='M')




If month names are needed, use strftime:



df['date'].dt.strftime('%Y-%b').unique()
# array(['2012-Jan', '2016-Dec'], dtype=object)


If Series format is necessary, use drop_duplicates:



df['date'].dt.strftime('%Y-%b').drop_duplicates()

0 2012-Jan
18 2016-Dec
Name: date, dtype: object






share|improve this answer












share|improve this answer



share|improve this answer










answered Dec 28 '18 at 19:45









coldspeedcoldspeed

124k22125208




124k22125208













  • @W-B Wow, did not notice such a thread... interesting indeed!

    – coldspeed
    Dec 28 '18 at 19:51






  • 1





    Just Jez, you and Pir are too good and' jealous people'

    – W-B
    Dec 28 '18 at 19:52













  • @W-B Add yourself to that list please, you are one of us and have been so for a long time.

    – coldspeed
    Dec 28 '18 at 19:53






  • 1





    Nope I only have limit years experience with python and learn from your guys :-)

    – W-B
    Dec 28 '18 at 19:54








  • 1





    Just catch an good answer there

    – W-B
    Dec 28 '18 at 20:07



















  • @W-B Wow, did not notice such a thread... interesting indeed!

    – coldspeed
    Dec 28 '18 at 19:51






  • 1





    Just Jez, you and Pir are too good and' jealous people'

    – W-B
    Dec 28 '18 at 19:52













  • @W-B Add yourself to that list please, you are one of us and have been so for a long time.

    – coldspeed
    Dec 28 '18 at 19:53






  • 1





    Nope I only have limit years experience with python and learn from your guys :-)

    – W-B
    Dec 28 '18 at 19:54








  • 1





    Just catch an good answer there

    – W-B
    Dec 28 '18 at 20:07

















@W-B Wow, did not notice such a thread... interesting indeed!

– coldspeed
Dec 28 '18 at 19:51





@W-B Wow, did not notice such a thread... interesting indeed!

– coldspeed
Dec 28 '18 at 19:51




1




1





Just Jez, you and Pir are too good and' jealous people'

– W-B
Dec 28 '18 at 19:52







Just Jez, you and Pir are too good and' jealous people'

– W-B
Dec 28 '18 at 19:52















@W-B Add yourself to that list please, you are one of us and have been so for a long time.

– coldspeed
Dec 28 '18 at 19:53





@W-B Add yourself to that list please, you are one of us and have been so for a long time.

– coldspeed
Dec 28 '18 at 19:53




1




1





Nope I only have limit years experience with python and learn from your guys :-)

– W-B
Dec 28 '18 at 19:54







Nope I only have limit years experience with python and learn from your guys :-)

– W-B
Dec 28 '18 at 19:54






1




1





Just catch an good answer there

– W-B
Dec 28 '18 at 20:07





Just catch an good answer there

– W-B
Dec 28 '18 at 20:07


















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%2f53963553%2fextract-unique-monthly-periods-from-pandas-datetime-column%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







Popular posts from this blog

Angular Downloading a file using contenturl with Basic Authentication

Olmecas

Can't read property showImagePicker of undefined in react native iOS