# How to write Python

## Python code

The main goal is to put the code result you want to get into the `result` as a `pandas.DataFrame` object.

Your code will be wrapped by the following codes and be executed in this form. Be aware that `import` is **not allowed** in your code.

```python
import math
import numpy as np
import pandas as pd
import time
import datetime
import decimal
import get_raw_data, get_raw_data_in_df # functions to get the time-series data

# The object you have to put your code's result into
result : pd.DataFrame


'''
You code runs here
'''


# Convert result into JSON array.
# The structure should be the table with the columns.

return result
        .where(pd.notnull(code_dict["result"]), None)
        .replace({np.nan: None})
        .to_dict(orient="records")


```

***

## How to get market data

Unlike the [function mode](/analytics/create-chart/function.md), you will import the data you need in the **python code**. We provide some functions to import the them.

### 1. get\_raw\_data

`get_raw_data` is a function to get the data you can find in [data preview](/terminal/create-bot-strategy/1.-how-to-preview-data.md). It returns the data as an array of dictionaries. It requires four arguments; `table_name`, `symbol`, `start_time`, and `end_time`.

Check [**here**](https://docs.unblinked.com/analytics/create-chart/python/2.-how-to-analyze-data/pages/kZ8hy0a23Eo4qUWgraCg#1.-get-the-time-series-data) for more detail.

***

### 2. get\_raw\_data\_in\_df

`get_raw_data_in_df` is same as `get_raw_data`, but it refines the result into `pandas.DataFrame` type, and converts `decimal.Decimal` type values in it into `float`.

It is useful to apply methods from pandas such as `ewm` to the data.

Check [**here**](https://docs.unblinked.com/analytics/create-chart/python/2.-how-to-analyze-data/pages/kZ8hy0a23Eo4qUWgraCg#2.-get-the-time-series-data-in-dataframe) for more detail.

***

## Information&#x20;

Python version : 3.11.1

pandas : 2.0.0

numpy : 1.24.2


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.unblinked.com/analytics/create-chart/python/2.-how-to-analyze-data/how-to-write-python.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
