Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/346.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使日期列在postgres而不是Python中对数据探索更具吸引力?_Python_Postgresql - Fatal编程技术网

如何使日期列在postgres而不是Python中对数据探索更具吸引力?

如何使日期列在postgres而不是Python中对数据探索更具吸引力?,python,postgresql,Python,Postgresql,我有一张股市数据图表,是用Plotly Dash绘制的。我画x表示日期,y表示价格。在将x数据绘制为日期时,标签对于数据探索来说总是不稳定和怪异的 例如: 正如你所看到的,x轴是一系列的一月和七月月份。这是自然自动生成的,对于用户体验来说并不好看。最好是2015年第一季度、2015年第二季度或2016年冬季、2016年夏季等等 在Python中,我可以通过如下类似方式编辑数据帧来实现这一点: for column in lst: column.loc[column["mont

我有一张股市数据图表,是用Plotly Dash绘制的。我画x表示日期,y表示价格。在将x数据绘制为日期时,标签对于数据探索来说总是不稳定和怪异的

例如:

正如你所看到的,x轴是一系列的一月和七月月份。这是自然自动生成的,对于用户体验来说并不好看。最好是2015年第一季度、2015年第二季度或2016年冬季、2016年夏季等等

在Python中,我可以通过如下类似方式编辑数据帧来实现这一点:

for column in lst:
    column.loc[column["month_int"] == 1, "month"] = "January"
    column.loc[column["month_int"] == 2, "month"] = "February"
    column.loc[column["month_int"] == 3, "month"] = "March"
    column.loc[column["month_int"] == 4, "month"] = "April"
    column.loc[column["month_int"] == 5, "month"] = "May"
    column.loc[column["month_int"] == 6, "month"] = "June"
    column.loc[column["month_int"] == 7, "month"] = "July"
    column.loc[column["month_int"] == 8, "month"] = "August"
    column.loc[column["month_int"] == 9, "month"] = "September"
    column.loc[column["month_int"] == 10, "month"] = "October"
    column.loc[column["month_int"] == 11, "month"] = "November"
    column.loc[column["month_int"] == 12, "month"] = "December"
    
# Or like this     

for column in lst2:
    column.loc[(column['month_int'] > 2) & (column['month_int'] <= 5), 'Season'] = 'Spring'
    column.loc[(column['month_int'] > 5) & (column['month_int'] <= 8), 'Season'] = 'Summer'
    column.loc[(column['month_int'] > 8) & (column['month_int'] <= 11), 'Season'] = 'Autumn'
    column.loc[column['month_int'] <= 2, 'Season'] = 'Winter'
    column.loc[column['month_int'] == 12, 'Season'] = 'Winter

要获取月份,可以在postgres中使用
TO_CHAR
函数

select symbol, date, to_char(date, 'Month') as month, adj_close from api.security_price 
WHERE security_price.symbol IN %s AND date > (SELECT MAX(date) FROM api.security_price) - interval '5 years' 
ORDER by date;
参考:

同样,您也可以为特殊情况定义自己的sql查询函数。 参考号:


对于季节,另一种简单的方法是使用另一列,其中包含一个月到季节的映射,并在输出数据上与该表进行左联接。

添加新添加项会使我的查询失败,有什么值得注意的原因吗?我的年数有一个奇怪的值?试着把所有对
date
的引用都用双引号引起来。这能解决问题吗?我们看不到奇怪!请发布查询结果及其产生的结果。
select symbol, date, to_char(date, 'Month') as month, adj_close from api.security_price 
WHERE security_price.symbol IN %s AND date > (SELECT MAX(date) FROM api.security_price) - interval '5 years' 
ORDER by date;