Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I'm writting (just for fun) a function that prints calendar for any given month and year.
How to determine the first day of a month? Without using the calendar module.
Do you mean week day (monday, ... sunday)?
>>> from datetime import datetime
>>> datetime(2017, 11, 2).weekday() # Or another day
3
Results from 0 to 6, where 0 is monday.
http://docs.python.org/3/library/datetime.html
There's also isoweekday, isocalendar and other functions/methods in the link above that might be helpful.
Assuming you want to know how the computation is done, and not just what library module to call, the usual method is Zeller's Congruence.
http://en.wikipedia.org/wiki/Zeller%27s_congruence
That's a neat formula, based on the observation that the days in the months are pretty much regularly-spaced if you view the year as starting in March instead of January. If you look at the Latin behind the names of the months from September to December, you should see that that indeed is when the year started when the (then Julian, named for Julius Caesar) calendar was devised.
You can also search for that term to find a large number of implementation, of varying quality, in just about every programming language.
H.D's answer is good, if you need the name of the day the following should work.
>>> from datetime import datetime
>>> datetime.now().strftime("%A")
'Wednesday'
>>>
Related
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 1 year ago.
Improve this question
I have a bank data having dates and amount,
and a holiday csv file given separately
which has dates of holiday and I have to add the
amount values from date of holiday to the next
working day and make the amount of the day
having holiday '0'
I'll try to answer generically as there's no other info provided.
As the question is "How to Exclude Holidays and Weekends from a Bank data in python" there are multiple ways how to achieve it:
create a new list, iterate through old list's elements and add to the new one only those that match a certain condition
filter already existing list with a code that will be executed for each element
etc
As you have the bank data in CSV, you need to convert it to a format Python can understand directly - that is the dict you've mentioned in the comments, or any other Python structure as CSV is basically text and worse to manipulate directly. Thus bank data would be one dict and the holiday data would be another dict.
Then you apply the logic via programming so that the holidays and weekends aren't present in those items.
Also, I recommend you to start splitting problems into smaller parts until you can address at least one of the parts, then incrementally move forward with resolving it until eventually you solve the whole problem.
Some tutorials worth checking:
https://docs.python.org/3/tutorial/index.html
https://python-guide.org
https://automatetheboringstuff.com/
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I'm doing a neural network project in which there is a feature that is the time passed from the user's last activity until some specific times. For example imagine that there is a list of times (March 15th, April 1st, April 24th, etc) and we want to find the interval between each user last activity before any of those specific times and the specific time. To be more clear, image user1 has some actions on March 10th, March 13th and March 24th, the values for him/her according to March 15th would be 2 days (March 13th). Now what if the user has no actions before March 15th?
Now due to some algorithms, I am joining some temp tables which result in having lots of NaN. How to tell the network that these cells should not be considered?
edit1
the code to fill the cell is this:
for aciton_time in all_aciton_times:
interval_tmp = actions_df.loc[(actions_df['when'] < aciton_time)].drop_duplicates(subset="device_id", keep='last')
interval_tmp['aciton_' + str(aciton_time)] = interval_tmp['when'].apply(lambda x: aciton_time - x)
del interval_tmp['when']
interval = interval.merge(interval_tmp, on="device_id", how="outer")
previous_aciton_time = aciton_time
and the result is something like this:
thanks
If you have a large dataset you could drop any rows that have NaN values
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
I have a csv file of stock prices for each trading day for 9 years. how do i get the last trading day of each month and the respective prices?
I have tried grouping by months followed by the largest day but it doesn't seem to be working. Any guidance or suggestions pls
file:///var/folders/76/qqn_44f945564bdvv8dw_0jc0000gn/T/com.apple.Safari/WebKitDropDestination-wOBqM5Fs/Screenshot%202019-08-19%20at%205.03.22%20PM.png
import pandas as pd
data=pd.read_csv('csv_file')
data
type(data.index)
data.set_index('date',inplace=True)
Apologies, its my first time using this so i don't really know how to post the code. But this is the code i have so far. The url is the result of the csv data.
You can use
df.groupby([pd.Grouper(key = 'column_containing date', freq = 'M')])['column_containing date'].last()
If your date data is part of the index you can use
df.groupby(df.index.strftime('%Y-%m')).tail(1)
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
In my dataset there is an independent column called "Cycle". It has date values written in text format. I am not understanding how to convert it into numbers. I am working with Multiple Linear Regression and Python. The Column looks like this. Any idea regarding this.
Cycle
10th June to 11th July
20th June to 21st July
17th June to 18th July
Any idea regarding this
Disclaimer: Since you question is broad and quite vague on details answer is aimed at only at pointing you where to research and some general terms around it.
This is example of categorical data. In a nutshell you can do several things with it, here are presented some ideas:
If categories are fixed and you know all possible values you can convert them to numerical values by assigning each of them incremental (or new random) number.
If your categories are not known in advance you can convert them to hashed category. As a variation of this approach you can hash only most frequent categories and summary reduce outliers to smaller number of hash values reducing total amount of categories used.
You can bucketize them, depending on your expected impact and here are just some ideas:
bucketize in month chunks:
bucketize in quartal chunks
bucketize in weeks chunks
Finally you can transform it to more detailed representation, getting
additional value out of it like so (this is just an example):
Cycle C_Start_Day C_Start_Month C_End_Day C_End_Month C_Num_Days
10th June to 11th July 10 6 11 7 1
20th June to 21st July 20 6 21 7 1
17th June to 18th July 17 6 18 7 1
Note: based on your previous comment I'd suggest using last approach (transformation). that way you can extract from Cycle column all the data you need for further numerical processing.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
Coding in Python
I have a CSV file with a column called time that consists of strings like 12:00:00 AM and 10:00:00 PM. I simply want to convert these strings into their corresponding military time representation either by string or integer such that 12:00:00 AM becomes "0" or 0 and 10:00:00 PM becomes "22" or 22.
I am new to coding in general so I have no idea what are the correct keywords to search for. Thanks!
Try coding it first by yourself and update your question by posting your code when you encounter errors with it.
To give you a head start, you can start by reading the time module of python. In particular, you will be needing the time.strptime to convert your string format to a time format then output your desired military time format using time.strftime.
Make sure to check out the directives properly for the formatting of your strings. Happy coding!
import datetime
t = '12:00:00 AM'
t_dt = datetime.datetime.strptime(t, '%I:%M:%S %p')
then you can access the hour with t_dt.hour