简体   繁体   English

是否可以通过将当月的天数减少到 1 来从 SQLAlchemy 中的日期时间中提取一个月,以便 plot 图表中的数据?

[英]Is it possible to extract a month from a datetime in SQLAlchemy by reducing the days to 1 in that month in order to plot the data in a graph?

I know it is possible to extract a month from a datetime in sqlalchemy as an integer with:我知道可以从 sqlalchemy 中的日期时间中提取一个月作为 integer :

func.extract('month', datetime)

but is it possible to change the days of the datetime to 1 in that month in a Postgres database in order to plot the data in a graph like with:但是是否可以在 Postgres 数据库中将日期时间的天数更改为该月的 1,以便 plot 图表中的数据,如下所示:

func.strftime(datetime, '%Y-%m')

which doesn't work for Postgres databases?哪个不适用于 Postgres 数据库?

I have tried:我努力了:

func.to_char

but I need to get the datetime or at least the date grouped by month or week in order to plot the data over time in graphs and this doesn't seem to get a datetime unless I am doing something wrong.但我需要获取日期时间或至少按月或周分组的日期,以便 plot 图表中随时间推移的数据,除非我做错了什么,否则这似乎没有日期时间。 Any ideas?有任何想法吗?

I know that it is changing a datetime to a string, but for some reason the func.strftime worked for plotting the graphs with SQLite but func.char doesn't for Postgres.我知道它将日期时间更改为字符串,但由于某种原因,func.strftime 可以使用 SQLite 绘制图形,但 func.char 不适用于 Postgres。

Would I have to just create 3 seperate columns for week, month and year and then combine them into a datetime in order to plot a graph of data that I need grouped by weeks or months?我是否必须为周、月和年创建 3 个单独的列,然后将它们组合成一个日期时间,以便 plot 我需要按周或月分组的数据图表?

There are several approaches for this, depending on which database you're using.有几种方法可以解决这个问题,具体取决于您使用的数据库。 I've included a solution for postgres and mysql to demonstrate:我已经为 postgres 和 mysql 提供了一个解决方案来演示:

Postgres: Postgres:

# Can be used to truncate a datetime value according to any interval
func.date_trunc('month', date_col)

MySql: MySql:

# This reformats to the nearest month
# you can manually format to the first day of the month
func.date_format(date_col, '%Y-%m-01')

I have seperated the week, month and year into seperate columns and queried the data.我已将周、月和年分成单独的列并查询数据。 I ended up doing the calculations outside of sqlalchemy with the query result as I couldn't work it out inside the query.我最终用查询结果在 sqlalchemy 之外进行了计算,因为我无法在查询中计算出来。 I have used this example to help me reach an answer - Get date from week number我用这个例子来帮助我找到答案 - 从周数获取日期

I set the day to "1" represented as %w in the format for the data I got from the sqlalchemy query that grouped by week and year我将日期设置为“1”,以我从 sqlalchemy 查询中获得的数据格式表示为 %w,该查询按周和年分组

datetime_by_week = [datetime.strptime(f'{int(i.year)}-{int(i.week)}-1', '%Y-%W-%w').date() for i in data]

For the month I got the month number and year number after grouping by month and year in the sqlalchemy query.对于月份,我在 sqlalchemy 查询中按月份和年份分组后得到了月份和年份。

datetime_by_month = [datetime.strptime(f'{int(i.year)}-{int(i.month)}-01', '%Y-%m-%d').date() for i in data]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM