Create list based on actual month and year - pandas

I have my code which should returns names of the months from now and the year for the next 12 months.
e.g. whe have now September so the code should retuns list of months with year till the September 2023th.
month_names = "January February March April May June July August September October November December".split()
Year = '2022'
month_now = datetime.date.today().month
dict_of_dfs = {}
for i in range(month_now,len(month_names)):
df_name = month_names[i]
print(Year,i+1,'01')
This code returns only the months till the end of the year and I do not know how to change it.
The output should look like that:
2022 10 01
2022 11 01
2022 12 01
2023 01 01
2023 02 01
2023 03 01
...
2023 07 01
2023 08 01
2023 09 01

Check pd.date_range
pd.date_range(start = Year + '-' + str(month_now+1) + '-01', periods=12, freq='MS')

Another solution with pd.date_range:
pd.date_range(start=month_now.replace(day=1), periods=13, freq='MS')[1:]

Using Pendulum:
import pendulum
date_list = [pendulum.now().add(months=1).start_of("month").add(months=x).to_date_string() for x in range(12)]
print(date_list)
['2022-10-01', '2022-11-01', '2022-12-01', '2023-01-01', '2023-02-01', '2023-03-01', '2023-04-01', '2023-05-01', '2023-06-01', '2023-07-01', '2023-08-01', '2023-09-01']

Related

Is there a way to rejig a dataframe to show a better time series dataset?

Hi I have the following df:
Variable Total Month
Year
2011 110 01
2011 111 02
2011 112 03
2011 113 04
2011 114 05
2011 115 06
....
....
2021 302 04
2021 303 05
2021 304 06
Is it possible to rejig the dataset to this:
Jan Feb Mar Apr May .... Nov Dec
Year
2011 110 111 112 113 114
2012 ...
2013 ...
2014 ...
2015 ...
....
2020
2021
** I would also like to remove the "Variable" word at the corner of the table.
My eventual goal is to do some simple data visualization using matplotlib to create line plots of the respective years (2011...2021)
Thank you in advance!
Use pivot()+reindex():
from calendar import month_abbr
df['Month']=pd.to_datetime(df['Month'],format='%m').dt.strftime('%b')
df=df.pivot(columns='Month',values='Total').rename_axis(columns=None)
df=df.reindex(columns=month_abbr[1:])
OR
via pivot()+pd.Categorical():
df['Month']=pd.to_datetime(df['Month'],format='%m').dt.strftime('%b')
df=df.pivot(columns='Month',values='Total').rename_axis(columns=None)
df.columns=pd.Categorical(df.columns,month_abbr[1:],ordered=True)
df=df.sort_index(axis=1)
Now if you print df you will get your expected output

How to calculate median monthly from date of month table?

My dataset:
Date Num_orders
Mar 21 2019 69
Mar 22 2019 82
Mar 24 2019 312
Mar 25 2019 199
Mar 26 2019 2,629
Mar 27 2019 2,819
Mar 28 2019 3,123
Mar 29 2019 3,332
Mar 30 2019 1,863
Mar 31 2019 1,097
Apr 01 2019 1,578
Apr 02 2019 2,353
Apr 03 2019 2,768
Apr 04 2019 2,648
Apr 05 2019 3,192
Apr 06 2019 2,363
Apr 07 2019 1,578
Apr 08 2019 3,090
Apr 09 2019 3,814
Apr 10 2019 3,836
...
I need to calculate the monthly median number of orders from days of the same month:
The desired results:
Month Median_monthly
Mar 2019 1,863
Apr 2019 2,768
May 2019 2,876
Jun 2019 ...
...
I tried to use function date_trunc to extract month from the dataset then group by 'month' but it didn't work out. Thanks for your help, I use Google Bigquery (#standard) environment!
Probably you tried to use PERCENTILE_CONT which can not be used with GROUP BY:
Try to use APPROX_QUANTILES(x, 100)[OFFSET(50)]. It should work with GROUP BY.
SELECT APPROX_QUANTILES](Num_orders, 100)\[OFFSET(50)\] AS median
FROM myTable
GROUP BY Month
Alternativele you can use PERCENTILE_CONT within subquery:
SELECT
DISTINCT Month, median
FROM (
SELECT
Month,
PERCENTILE_CONT(Num_orders, 0.5) OVER(PARTITION BY Month) AS median
FROM myTable
)
This would often be done using DISTINCT:
SELECT DISTINCT DATE_TRUNC(month, date),
PERCENTILE_CONT(Num_orders, 0.5) OVER (PARTITION BY DATE_TRUNC(month, date) AS median
FROM myTable;
Note: There are two percentile functions, PERCENTILE_CONT() and PERCENTILE_DISC(). They have different results when there is a "tie" in the middle of the data.

How to extract dates from string fields in SQL?

I have field that contains strings and has dates within.
e.g
Rate (20 Jan 2020 - 19 Feb 2020)
or Rate (6 Dec 2019 - 5 Jan 2020)
I need a Start Date and End Date out of the above strings in SQL.
I can get Start Date but End Date (after the -) is a problem
Its quite crude but it will get you answers:
select left('20 Jan 2020 - 19 Feb 2020',CHARINDEX('-','20 Jan 2020 - 19 Feb 2020')-1)
,right('20 Jan 2020 - 19 Feb 2020',len('20 Jan 2020 - 19 Feb 2020') -CHARINDEX('-','20 Jan 2020 - 19 Feb 2020')-1)
CHARINDEX() will give you the position of the character you desire, in this case the dash. From there you can use LEFT(), RIGHT(), and LEN() to get the pieces out of the string that you need.
This works with SQLServer

getting first day and last day of a quarter and 2 quarters back for a date

how to get first day and last day of a quarter for a date?
and also first day and last day of 2 quarters back for a date in Hive or sql
for example for Feb 03 2014 first day and last day of the quarter will be
Jan 01 2014 and Mar 31 2014
and for the same date first and last day of 2 quarters back will be Jul 01 2013 and Sep 31 2013
You can accomplish this in the following way (not too fancy, but there is no direct way). To make it simpler, I just concatenated both output dates
-- before Hive 1.3
select
case
when ceil(month(mydate)/ 3.0) = 1 then concat("Jan 01 ",year(mydate),"|","Mar 31 ",year(mydate))
when ceil(month(mydate)/ 3.0) = 2 then then concat("Apr 01 ",year(mydate),"|","Jun 30 ",year(mydate))
when ceil(month(mydate)/ 3.0) = 3 then then concat("Jul 01 ",year(mydate),"|","Sep 30 ",year(mydate))
when ceil(month(mydate)/ 3.0) = 4 then then concat("Oct 01 ",year(mydate),"|","Dec 31 ",year(mydate))
else
null
end,
ceil(month(mydate)) as quarter
from (
select
from_unixtime(unix_timestamp('Feb 03 2014' , 'MMM dd yyyy')) as mydate
) t;
--Hive 1.3 or higher
select
case
when quarter(mydate) = 1 then concat("Jan 01 ",year(mydate),"|","Mar 31 ",year(mydate))
when quarter(mydate) = 2 then then concat("Apr 01 ",year(mydate),"|","Jun 30 ",year(mydate))
when quarter(mydate) = 3 then then concat("Jul 01 ",year(mydate),"|","Sep 30 ",year(mydate))
when quarter(mydate) = 4 then then concat("Oct 01 ",year(mydate),"|","Dec 31 ",year(mydate))
else
null
end,
ceil(month(mydate)) as quarter
from (
select
from_unixtime(unix_timestamp('Feb 03 2014' , 'MMM dd yyyy')) as mydate
) t;
just replace the hardcoded date for your column in the select in the inner query

How get dates on week day?

I wanted to ask about how to get date on weekday
green is weekday
red is sunday
so when i input sql command it (like 27) when year 2012
it will show date 2012-07-2 until 2012-07-08
This query uses a single parameter #weekno as input and returns the 7 days in that week, taking Monday as the first day of week. The definition of WeekNo does not follow SQL Server's DatePart(Week) because that depends on ##Datefirst. This doesn't.
The dateadd.. line is an expression that returns the first Monday of the year. I got it from here. The line above it just adds the weeks to it and 0-6 to create 7 days. To verify this is correct for any year, change CURRENT_TIMESTAMP in the query to a date, such as 20180708. FYI, 1-Jan-2018 is a Monday.
declare #weekno int = 27;
select
(#weekno-1)*7+v.num+
dateadd(dd,(datediff(dd,0,dateadd(yy,datediff(yy,0,CURRENT_TIMESTAMP),6))/7)*7,0)
from (values(0),(1),(2),(3),(4),(5),(6))v(num)
order by num
-- results
July, 02 2012 00:00:00+0000
July, 03 2012 00:00:00+0000
July, 04 2012 00:00:00+0000
July, 05 2012 00:00:00+0000
July, 06 2012 00:00:00+0000
July, 07 2012 00:00:00+0000
July, 08 2012 00:00:00+0000