user2100039
user2100039

Reputation: 1366

Arrange Pandas Data in 12-month Date Sequence From "Next" Month

I cannot figure out how to or know how to begin to arrange a pandas df with an index in datetime format in a 12-month sequence based on the "next" month from the current month. For example, if the current OR start month is "Feb" or in serial month form == 2 and the "next" OR target month is "Mar" or "3" then I need a 12-month date sequence arranged in the following serial month form: 4,5,6,7,8,9,10,11,12,1,2,3. My pandas data "df1" looks like this below where the index column is in datetime format. This is a small subset of 30 years of monthly data in this format.

date    month   std anomaly
2/1/1992 0:00   2   0.2
3/1/1992 0:00   3   3.2
4/1/1992 0:00   4   1.3
5/1/1992 0:00   5   0.8
6/1/1992 0:00   6   -0.3
7/1/1992 0:00   7   0.9
8/1/1992 0:00   8   1.0
9/1/1992 0:00   9   -0.4
10/1/1992 0:00  10  -0.9
11/1/1992 0:00  11  2.9
12/1/1992 0:00  12  0.8
1/1/1993 0:00   1   2.1
2/1/1993 0:00   2   1.5
3/1/1993 0:00   3   -0.7
4/1/1993 0:00   4   3.7
5/1/1993 0:00   5   1.6
6/1/1993 0:00   6   2.7
7/1/1993 0:00   7   -2.6
8/1/1993 0:00   8   3.5
9/1/1993 0:00   9   -2.9
10/1/1993 0:00  10  -2.8
11/1/1993 0:00  11  2.6
12/1/1993 0:00  12  1.3
1/1/1994 0:00   1   2.4
2/1/1994 0:00   2   2.3
3/1/1994 0:00   3   0.6

I need the newly-arranged data in "df2" to look like this:

date    month   std anomaly
4/1/1992 0:00   4   1.3
5/1/1992 0:00   5   0.8
6/1/1992 0:00   6   -0.3
7/1/1992 0:00   7   0.9
8/1/1992 0:00   8   1.0
9/1/1992 0:00   9   -0.4
10/1/1992 0:00  10  -0.9
11/1/1992 0:00  11  2.9
12/1/1992 0:00  12  0.8
1/1/1993 0:00   1   2.1
2/1/1993 0:00   2   1.5
3/1/1993 0:00   3   -0.7
4/1/1993 0:00   4   3.7
5/1/1993 0:00   5   1.6
6/1/1993 0:00   6   2.7
7/1/1993 0:00   7   -2.6
8/1/1993 0:00   8   3.5
9/1/1993 0:00   9   -2.9
10/1/1993 0:00  10  -2.8
11/1/1993 0:00  11  2.6
12/1/1993 0:00  12  1.3
1/1/1994 0:00   1   2.4
2/1/1994 0:00   2   2.3
3/1/1994 0:00   3   0.6

thank you very much on how to proceed to sort. I have tried generating a new date list based on the start and end dates that I could use to sort my original df "df1" with something like below but I get a new sorted "df1" original dataframe with all values == NaN.

month_list=[i.strftime("%b-%y") for i in pd.date_range(start="1992-04-01",end="2022-02-02",freq="MS")]

And using this -

df1.reindex(month_list)

Upvotes: 0

Views: 101

Answers (1)

user2100039
user2100039

Reputation: 1366

Ok, I figured it out. the answer lies in formatting the new month_list to the same format that my date column in the index column of my original df "df1" is in. Then, use that new "month_list" to reorder the data.

month_list=[i.strftime("%Y-%m-%d %H:%M:%S") for i in pd.date_range(start="1992-04-01",end="2022-02-02",freq="MS")]  

And, then use this - df1.reindex(month_list)

Upvotes: 0

Related Questions