Coder Social home page Coder Social logo

redash_pandas's Introduction

概要

Redash からデータを API 経由で取得するためのモジュール。

使い方

セットアップ

Colab などで使う場合:

!pip install git+https://github.com/alexweberk/redash_pandas.git

をした上で、

from redash_pandas import Redash

# クレデンシャルファイルを使う場合
redash_credentials = '<<クレデンシャルファイルへのパス>>' # JSONフォーマットでAPIキーなどを保存。その保存先を指定。
redash = Redash(credentials=redash_credentials)

もしくは、

# api_keyとendpointを直接指定する場合
redash = Redash(
    apikey="YOUR_API_KEY",
    endpoint="https://redash.example.com"
)

クエリの仕方

クエリの負荷が大きくない場合

df = redash.query(42) # query IDを数字で入れると、そのクエリ結果がpandasのDataFrameとして返ってくる。

クエリの負荷が大きい場合

# 行数が多い場合も、クエリ内に `limit_rows` と `offset_rows` というパラメータをつけてあげれば、指定した行数毎に
# 全てのデータを取得してくれる。
df = redash.safe_query(2674, params={'user_name':'John Doe', 'email':'[email protected]'}, limit=100_000)

クエリ取得を期間で絞れる場合

# 2023-01-01から2024-06-20までのデータに関して、3ヶ月単位でデータを取得する
df = redash.period_limited_query(6738, start_date='2023-01-01', end_date='2024-06-20',
            interval='month', interval_multiple = 3)

# 2023-01-01から2024-06-20までのデータに関して、4週間単位でデータを取得する
df = redash.period_limited_query(6738, start_date='2023-01-01', end_date='2024-06-20',
            interval='week', interval_multiple = 4)

クエリ側では、下記のような書き方をする必要がある。

select
    date_trunc('month', bookings.created_at + interval '9 hours') b_mo
    , count(distinct bookings.id) b_cnt
    , sum(bookings.price) b_price
from bookings
where true
    and bookings.status = 1
    and bookings.created_at + interval '9 hours' between '{{start_date}}'::date 
        and '{{end_date}}'::date - interval '1 second'
group by 1
order by 1

redash_pandas's People

Contributors

alexweberk avatar greasyslug avatar

Watchers

 avatar  avatar  avatar

Forkers

greasyslug

redash_pandas's Issues

Request for New Features for safe_query

Author: Bard

Describe

The safe_query method by default repeats 100 times, querying 10,000 rows each time. If each query takes 5-8 seconds, this can take over 10 minutes.

I request the following new features:

  • Save the progress
    • If possible, resume the query at the specified query number (row number) when an error occurs.
  • Display progress bar or count of query

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.