whole column as datetime.date(Y,m,d) in python pandas [duplicate] - python

This question already has answers here:
How to change the datetime format in Pandas
(8 answers)
Keep only date part when using pandas.to_datetime
(13 answers)
Closed 4 years ago.
When I read data from database (or csv, etc.) pandas keep storing date-only data as timestamp and adds 00:00:00? Is there a way to enforce that the column is of type datetime.date and therefore no time is stored?
I tried this but it seems the 00:00:00 is more sticky than originally thought.
pd.to_datetime(mydf['date'], format='%Y-%m-%d').iloc[0]
Out[68]: Timestamp('2015-03-27 00:00:00')
Can i have instead of Timestamp the whole column be of type just datetime.date?

Related

Final value in dataframe showing as not inside dataframe when going through for loop [duplicate]

This question already has answers here:
Python Pandas -- why does the `in` operator work with indices and not with the data?
(1 answer)
How to determine whether a Pandas Column contains a particular value
(11 answers)
Closed 1 year ago.
I'm checking to see if all the values in one column of one dataframe lies in the column of another dataframe. When I run the code below, it says that 4 does not exist in df1. Is there any particular reason for this?
list1=[1,2,3,4]
list2=[1,2,3,4]
df2=pd.DataFrame(list2)
df2.rename(columns={0:"List2"},inplace=True)
df1=pd.DataFrame(list1)
df1.rename(columns={0:"List1"},inplace=True)
for i in df2['List2']:
if i not in df1['List1']:
print(i)

How to query from multiindex dataframe containing data and time as index [duplicate]

This question already has answers here:
selecting from multi-index pandas
(7 answers)
Closed 1 year ago.
Check this image of dataframe
I've posted the picture of dataframe I am working with, I want to pull out data from specific times of a certain date
I've tried
stockdf.loc[("2015-01-01")].loc['09:17:00']
stockdf.loc[("2015-01-01","09:17:00"),:]
Both do not work.
Just try:
stockdf.loc[("2015-01-01", "09:17:00")]
If they're dates:
stockdf.loc[(pd.to_datetime("2015-01-01").date(), pd.to_datetime("09:17:00").time())]

Creating a column in Dataframe [duplicate]

This question already has answers here:
Add column with constant value to pandas dataframe [duplicate]
(4 answers)
Closed 3 years ago.
Very, very new to python and have a question:
Can someone tell me how to create a new date column that is the date this data was collected. For example, if this is from a Jan 1.xlsx file, this column should be full of Jan 1.
I know how to create the column but how do I populate with Jan 1? Right now I only have to do this with one file but I am going to have to do this for all 31 files for January.
All help greatly appreciated...
After you instatiate dataframe (read file into pandas object). Just do:
df["dt"]="Jan 1"
It will populate whole column with this 1 value, for all rows

How to convert SQL select statement with particular fields and where clause to pandas dataframe [duplicate]

This question already has answers here:
How do I sum values in a column that match a given condition using pandas?
(3 answers)
Pandas counting and summing specific conditions
(4 answers)
Closed 5 years ago.
I am trying to convert fallowing SQL statement into pandas dataframe in python
SELECT sum(money) from df where sex='female'
I am unable to get this in pandas
Thanks in advance
df.loc[df.sex=='female','money'].sum()

Date field in SAS imported in Python pandas Dataframe [duplicate]

This question already has answers here:
convert a SAS datetime in Pandas
(2 answers)
Closed 6 years ago.
I have imported a SAS dataset in python dataframe using Pandas read_sas(path)
function. REPORT_MONTH is a column in sas dataset defined and saved as DATE9. format. This field is imported as float64 datatype in dataframe and having numbers which is basically a sas internal numbers for storing a date in a sas dataset. Now wondering how can I convert this originally a date field into a date field in dataframe?
I don't know how python stores dates, but SAS stores dates as numbers, counting the number of days from Jan 1, 1960. Using that you should be able to convert it in python to a date variable somehow.
I'm fairly certain that when data is imported to python the formats aren't honoured so in this case it's easy to work around this, in others it may not be.
There's probably some sort of function in python to create a date of Jan 1, 1960 and then increment by the number of days you get from the imported dataset to get the correct date.

Categories

Resources