<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Magic functions
You can enable magic functions by loading pandas_td.ipython
Step1:  It can be loaded automatically by the following configuration in "~/.ipython/profile_default/ipython_config.py"
Step2:  After loading the extension, type "%td" and press TAB to list magic functions
Step3:  %td_tables returns the list of tables
Step4:  %td_jobs returns the list of recently executed jobs
Step5:  Use database
%td_use is a special function that has side effects.  First, it pushes table names into the current namespace
Step6:  By printing a table name, you can describe column names
Step7:  Tab completion is also supported
Step8:  The result of the query can be stored in a variable by -o
Step9:  Or you can save the result into a file by -O
Step10:  Python-style variable substition is supported
Step11:  You can preview the actual query by --dry-run (or -n)
Step12:  Time-series index
With magic functions, "time" column is converted into time-series index automatically.  You can use td_date_trunc() or td_time_format() in combination with GROUP BY for aggregation
Step13:  Plotting
--plot is a convenient option for plotting.  The first column represents x-axis.  Other columns represent y-axis
Step14:  In practice, however, it is more efficient to execute rough calculation on the server side and store the result into a variable for further analysis
Step15:  --plot provides a shortcut way of plotting "pivot charts", as a combination of pivot() and plot().  If the query result contains non-numeric columns, or column names ending with "_id", they are used as columns parameter
Step16:  Pivot tables
--pivot creates a pivot table from the result of query.  Like --plot, the first column represents index and other non-numeric columns represents new columns
Step17:  Verbose output
By passing -v (--verbose) option, you can print pseudo Python code that was executed by the magic function.
<ASSISTANT_TASK:>
Python Code:
%load_ext pandas_td.ipython
Explanation: Magic functions
You can enable magic functions by loading pandas_td.ipython:
End of explanation
c = get_config()
c.InteractiveShellApp.extensions = [
    'pandas_td.ipython',
]
Explanation: It can be loaded automatically by the following configuration in "~/.ipython/profile_default/ipython_config.py":
End of explanation
%td_databases
Explanation: After loading the extension, type "%td" and press TAB to list magic functions:
List functions
%td_databases returns the list of databases:
End of explanation
%td_tables sample
Explanation: %td_tables returns the list of tables:
End of explanation
%td_jobs
Explanation: %td_jobs returns the list of recently executed jobs:
End of explanation
%td_use sample_datasets
Explanation: Use database
%td_use is a special function that has side effects.  First, it pushes table names into the current namespace:
End of explanation
nasdaq
Explanation: By printing a table name, you can describe column names:
End of explanation
%%td_presto
select count(1) cnt
from nasdaq
Explanation: Tab completion is also supported:
As the second effect of %td_use, it implicitly changes "default database", which is used when you write queries without database names.
Query functions
%%td_hive, %%td_pig, and %%td_presto are cell magic functions that run queries:
End of explanation
%%td_presto -o df
select count(1) cnt
from nasdaq
df
Explanation: The result of the query can be stored in a variable by -o:
End of explanation
%%td_presto -O './output.csv'
select count(1) cnt
from nasdaq
Explanation: Or you can save the result into a file by -O:
End of explanation
start = '2010-01-01'
end = '2011-01-01'
%%td_presto
select count(1) cnt
from nasdaq
where td_time_range(time, '{start}', '{end}')
Explanation: Python-style variable substition is supported:
End of explanation
%%td_presto -n
select count(1) cnt
from nasdaq
where td_time_range(time, '{start}', '{end}')
Explanation: You can preview the actual query by --dry-run (or -n):
End of explanation
%%td_presto
select
    -- Time-series index (yearly)
    td_date_trunc('year', time) time,
    -- Same as above
    -- td_time_format(time, 'yyyy-01-01') time,
    count(1) cnt
from
    nasdaq
group by
    1
limit
    3
Explanation: Time-series index
With magic functions, "time" column is converted into time-series index automatically.  You can use td_date_trunc() or td_time_format() in combination with GROUP BY for aggregation:
End of explanation
%matplotlib inline
%%td_presto --plot
select
    -- x-axis
    td_date_trunc('year', time) time,
    -- y-axis
    min(low) low,
    max(high) high
from
    nasdaq
where
    symbol = 'AAPL'
group by
    1
Explanation: Plotting
--plot is a convenient option for plotting.  The first column represents x-axis.  Other columns represent y-axis:
End of explanation
%%td_presto -o df
select
    -- daily summary
    td_date_trunc('day', time) time,
    min(low) low,
    max(high) high,
    sum(volume) volume
from
    nasdaq
where
    symbol = 'AAPL'
group by
    1
# Use resample for local calculation
df['high'].resample('1m', how='max').plot()
Explanation: In practice, however, it is more efficient to execute rough calculation on the server side and store the result into a variable for further analysis:
End of explanation
%%td_presto --plot
select
    -- x-axis
    td_date_trunc('month', time) time,
    -- columns
    symbol,
    -- y-axis
    avg(close) close 
from
    nasdaq
where
    symbol in ('AAPL', 'MSFT')
group by
    1, 2
Explanation: --plot provides a shortcut way of plotting "pivot charts", as a combination of pivot() and plot().  If the query result contains non-numeric columns, or column names ending with "_id", they are used as columns parameter:
End of explanation
%%td_presto --pivot
select
    td_date_trunc('year', time) time,
    symbol,
    avg(close) close 
from
    nasdaq
where
    td_time_range(time, '2010', '2015')
    and symbol like 'AA%'
group by
    1, 2
Explanation: Pivot tables
--pivot creates a pivot table from the result of query.  Like --plot, the first column represents index and other non-numeric columns represents new columns:
End of explanation
%%td_presto -v --plot
select
    td_date_trunc('year', time) time,
    sum(volume) volume
from
    nasdaq
group by
    1
Explanation: Verbose output
By passing -v (--verbose) option, you can print pseudo Python code that was executed by the magic function.
End of explanation
<END_TASK>