[GH-ISSUE #1004] Spotify and Pandas Incompatability #599

Closed
opened 2026-02-28 00:00:01 +03:00 by kerem · 5 comments
Owner

Originally created by @PaulGeorge1993 on GitHub (Jul 21, 2023).
Original GitHub issue: https://github.com/spotipy-dev/spotipy/issues/1004

I am currently working on a code to read each song in a Spotify playlist and save the artist's name, song name, and duration into a CSV file. However, when running the code, I encounter the following error:

ImportError: Unable to import required dependencies:
numpy: cannot import name randbits

For reference, I have tested it and my code works well without the pandas import and I have created a virtual environment where the only modules present are spotipy and panda, yet, the error continues to persist. Is the issue that I need to use a different version of pandas (2.03), numpy (1.25.1), or spotipy ( 2.23.0), or is it simply a bug?

Originally created by @PaulGeorge1993 on GitHub (Jul 21, 2023). Original GitHub issue: https://github.com/spotipy-dev/spotipy/issues/1004 I am currently working on a code to read each song in a Spotify playlist and save the artist's name, song name, and duration into a CSV file. However, when running the code, I encounter the following error: > ImportError: Unable to import required dependencies: numpy: cannot import name randbits For reference, I have tested it and my code works well without the pandas import and I have created a virtual environment where the only modules present are spotipy and panda, yet, the error continues to persist. Is the issue that I need to use a different version of pandas (2.03), numpy (1.25.1), or spotipy ( 2.23.0), or is it simply a bug?
kerem 2026-02-28 00:00:01 +03:00
  • closed this issue
  • added the
    bug
    label
Author
Owner

@dieser-niko commented on GitHub (Jul 22, 2023):

Does this error have to do something with the spotipy library? It seems to be an issue in the pandas library.

<!-- gh-comment-id:1646361766 --> @dieser-niko commented on GitHub (Jul 22, 2023): Does this error have to do something with the spotipy library? It seems to be an issue in the pandas library.
Author
Owner

@CezarGab commented on GitHub (Aug 3, 2023):

Can you provide the line where this error appears? Also I thinks this is an import error, from pandas, and not Spotipy.

<!-- gh-comment-id:1664750836 --> @CezarGab commented on GitHub (Aug 3, 2023): Can you provide the line where this error appears? Also I thinks this is an import error, from pandas, and not Spotipy.
Author
Owner

@stephanebruckert commented on GitHub (Aug 4, 2023):

I have tested it and my code works well without the pandas import

Does it work with only pandas import?

<!-- gh-comment-id:1665040985 --> @stephanebruckert commented on GitHub (Aug 4, 2023): > I have tested it and my code works well without the pandas import Does it work with only pandas import?
Author
Owner

@dpnem commented on GitHub (Sep 10, 2023):

I use spotipy and pandas all the time together without any problems other than self-inflicted ones. Here's a function that builds a dataframe from a playlist_id. The way my processes work it goes from spotipty -> pandas -> lists -> spotipy.

def build_track_info_df_from_playlist(playlist_id=None):
    '''Given a playlist_id, this function returns a Pandas dataframe
    which contains the following track information:
    playlist_id, playlist_name, playlist_owner,
    track_id, track, artist_id, artist, album_id, album,
    release_date, release_decade, added_date'''

    if playlist_id is None:
        results = sp.current_user_saved_tracks()

        playlist_id = -10
        playlist_name = 'Liked Tracks'
        playlist_owner = 'You'

    else:
        header = sp.playlist(playlist_id)
        playlist_id = playlist_id
        playlist_name = header['name']
        playlist_owner = header['owner']['display_name']

        results = sp.playlist_tracks(playlist_id)
        
    complete_results = results['items']

    while results['next']:
        results = sp.next(results)
        complete_results.extend(results['items'])

    track_info = []
    
    for i, track in enumerate(complete_results):
        if track['track']['type'] == 'track':  # Exclude podcast episodes
            track_data = {
                'playlist_id': playlist_id, 
                'playlist_name': playlist_name,
                'playlist_owner': playlist_owner,
                'playlist_track_number': i+1,
                'track_id': track['track']['id'],
                'track': track['track']['name'],
                'artist_id': track['track']['artists'][0]['id'],
                'artist': track['track']['artists'][0]['name'],
                'album_id': track['track']['album']['id'],
                'album': track['track']['album']['name'],
                'popularity': track['track']['popularity'],
                'album_type': track['track']['album']['album_type'],
            }   

            # cleanup release date
            release_date = pd.to_datetime(track['track']['album']['release_date'])
            if pd.notna(release_date):
                track_data['release_date'] = release_date.strftime('%Y-%m-%d')
                track_data['release_decade'] = release_date.year // 10 * 10
                today = datetime.now()
                days_since_release = (today - release_date).days
                track_data['days_since_release'] = days_since_release
            else:
                track_data['release_date'] = today.strftime('%Y-%m-%d')
                track_data['release_decade'] = today.year // 10 * 10
                track_data['days_since_release'] = 0

            # clean up added_at
            added_date = pd.to_datetime(track['added_at'])
            track_data['added_date'] = added_date.strftime('%Y-%m-%d')

            track_info.append(track_data)

    df = pd.DataFrame(track_info)

    df['days_since_group'] = df['days_since_release'].apply(label_days)
    df['days_since_weight'] = 1 / df['days_since_release']


    return df
<!-- gh-comment-id:1712815076 --> @dpnem commented on GitHub (Sep 10, 2023): I use spotipy and pandas all the time together without any problems other than self-inflicted ones. Here's a function that builds a dataframe from a playlist_id. The way my processes work it goes from spotipty -> pandas -> lists -> spotipy. ```python3 def build_track_info_df_from_playlist(playlist_id=None): '''Given a playlist_id, this function returns a Pandas dataframe which contains the following track information: playlist_id, playlist_name, playlist_owner, track_id, track, artist_id, artist, album_id, album, release_date, release_decade, added_date''' if playlist_id is None: results = sp.current_user_saved_tracks() playlist_id = -10 playlist_name = 'Liked Tracks' playlist_owner = 'You' else: header = sp.playlist(playlist_id) playlist_id = playlist_id playlist_name = header['name'] playlist_owner = header['owner']['display_name'] results = sp.playlist_tracks(playlist_id) complete_results = results['items'] while results['next']: results = sp.next(results) complete_results.extend(results['items']) track_info = [] for i, track in enumerate(complete_results): if track['track']['type'] == 'track': # Exclude podcast episodes track_data = { 'playlist_id': playlist_id, 'playlist_name': playlist_name, 'playlist_owner': playlist_owner, 'playlist_track_number': i+1, 'track_id': track['track']['id'], 'track': track['track']['name'], 'artist_id': track['track']['artists'][0]['id'], 'artist': track['track']['artists'][0]['name'], 'album_id': track['track']['album']['id'], 'album': track['track']['album']['name'], 'popularity': track['track']['popularity'], 'album_type': track['track']['album']['album_type'], } # cleanup release date release_date = pd.to_datetime(track['track']['album']['release_date']) if pd.notna(release_date): track_data['release_date'] = release_date.strftime('%Y-%m-%d') track_data['release_decade'] = release_date.year // 10 * 10 today = datetime.now() days_since_release = (today - release_date).days track_data['days_since_release'] = days_since_release else: track_data['release_date'] = today.strftime('%Y-%m-%d') track_data['release_decade'] = today.year // 10 * 10 track_data['days_since_release'] = 0 # clean up added_at added_date = pd.to_datetime(track['added_at']) track_data['added_date'] = added_date.strftime('%Y-%m-%d') track_info.append(track_data) df = pd.DataFrame(track_info) df['days_since_group'] = df['days_since_release'].apply(label_days) df['days_since_weight'] = 1 / df['days_since_release'] return df ```
Author
Owner

@stephanebruckert commented on GitHub (Jul 9, 2024):

Closing as we have an example for it and no response from OP

<!-- gh-comment-id:2218355165 --> @stephanebruckert commented on GitHub (Jul 9, 2024): Closing as we have an example for it and no response from OP
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/spotipy#599
No description provided.