Python Forum
numpy newbie question - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: numpy newbie question (/thread-39375.html)



numpy newbie question - bcwilly_ca - Feb-08-2023

Hello:

Just started in python and have successfully connected to an application using its API

When I query the API for Data I get a list returned with each list item being one line of 22 comma-separated properties from the database

I have loaded the list into a numpy array but it is only one dimensional.. as in List[0]-> 22 comma separated values, List[1]-> 22 comma separated values

What is the easiest way to convert this into a 2D array [#listitems, 23] and load each property in its appropriate element in the array.

Ex:
List [0] = assigne_id=None, assigner_id=987, comment_count=0, and so on
List [1] = assigne_id=None, assigner_id=123, comment_count=0, and so on

My array would be
List [1,2] would return assigner_id=123

Bonus points on how I would just load the data in the array after the equals


RE: numpy newbie question - noisefloor - Feb-08-2023

Hi,

please do describe your problem by words. Show the actual data / data structure you get from the API call and to which structure you like to transform it.

Regards, noisefloor


RE: numpy newbie question - bcwilly_ca - Feb-08-2023

I am linking to Todoist. the API call is simple

from todoist_api_python.api import TodoistAPI
from datetime import datetime
import numpy as np

api = TodoistAPI("XXXXXXX")
try:
tasks = api.get_tasks()
tskarray = np.asarray(tasks)

the tasks come in as a list.. and then I load them in the array
if I print(tskarray[0]) I get

Task(assignee_id=None, assigner_id=None, comment_count=0, is_completed=False, content='Follow up: with Barry on Painting', created_at='2022-11-24T14:19:19.639931Z', creator_id='12345', description='', due=Due(date='2023-02-06', is_recurring=False, string='Feb 6', datetime=None, timezone=None), id='12345', labels=[], order=19, parent_id=None, priority=3, project_id='2277369185', section_id=None, url='https://todoist.com/showTask?id=12345', sync_id=None)

What I would like is a quick way to get
Tskarray [0,1] as assignee_id=None
Tskarray [0,2] as assigner_id=None
Tskarray [0,3] as comment_count=0
and so on

I really am thinking I would like
Tskarray [0,1] as None
Tskarray [0,2] as None
Tskarray [0,3] as 0
removing the indicator

I would do a for next loop to parse through the sting and load the array while also having a function remove the text from the left of the equals sign.

I am just wondering if there is a simple way in python to slit each of those comma-separated values out and put it in an array.


RE: numpy newbie question - noisefloor - Feb-09-2023

Hallo,

Quote:the tasks come in as a list..
As you still don't show what the API call return - how knows.

What you show as "Task(assignee_id=None, assigner_id=None, ...)" is an instance of a Task object, not a list.

Based on the bits and pieces you showed so far I'd also say the using Numpy is the wrong way to proceed. Although you basically can squeeze anything into a numpy Array, it mostly makes sense and is geared towards to numerical data.
For the shown data it makes more sense e.g. to put into a dict and walk through this or create an in-memory SQLite database, put all data into a table and query this.

Regards, noisefloor


RE: numpy newbie question - jefsummers - Feb-10-2023

OK, got you close -
Since numpy is a numerical oriented library, I decided to use Pandas.
import pandas as pd
from todoist_api_python.api import TodoistAPI

# Fetch tasks synchronously
def get_tasks_sync():
    api = TodoistAPI("XXXXXX")
    try:
        tasks = api.get_tasks()
        return tasks
    except Exception as error:
        print(error)

tasklist = get_tasks_sync()
itemlist = [] #start a list of the tasks
for task_obj in tasklist: # recognize that you have a set of Task objects to convert to strings
    task = task_obj.__str__() # convert to a string
    item = task[5:-1] # Get rid of "Task("
    item = item[0:-3] # Get rid of closing parens
    item = item.split(',') # Now split the string on commas into the individual fields
    itemlist.append(item) # Add this list of fields to the list of tasks
    
df = pd.DataFrame(itemlist) #Turn it into a dataframe
df.head() #show result