Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
numpy newbie question
#1
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
Reply
#2
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
Reply
#3
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.
Reply
#4
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
Reply
#5
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How is pandas modifying all rows in an assignment - python-newbie question markm74 1 705 Nov-28-2023, 10:36 PM
Last Post: deanhystad
  newbie question - can't make code work tronic72 2 696 Oct-22-2023, 09:08 PM
Last Post: tronic72
  Newbie question about switching between files - Python/Pycharm Busby222 3 623 Oct-15-2023, 03:16 PM
Last Post: deanhystad
  Newbie.... run for cover. OpenCV question Stevolution2023 2 990 Apr-12-2023, 12:57 PM
Last Post: Stevolution2023
  Question from complete python's newbie Davicom 3 2,381 Jun-09-2021, 06:09 PM
Last Post: bowlofred
  Newbie question about running Python via GUI on OSX ejwjohn 8 3,527 Feb-05-2021, 03:20 PM
Last Post: Larz60+
  super newbie question: escape character tsavoSG 3 2,484 Jan-13-2021, 04:31 AM
Last Post: tsavoSG
  newbie question....importing a created class ridgerunnersjw 5 2,679 Oct-01-2020, 07:59 PM
Last Post: ridgerunnersjw
  Dumb newbie question JonEdward 5 3,274 Jul-22-2020, 10:06 PM
Last Post: snippsat
  Newbie Question juljan 4 2,805 Jan-13-2020, 03:07 AM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020